springboot+vue日期工具类-企业排班
分类: Java 专栏: springboot+vue企业排班人脸批量热力图 标签: 日期工具类-企业排班
2026-02-16 15:27:05 54浏览
日期工具类-企业排班
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;
import java.util.UUID;
/**
* 日期工具类:提供日期格式化、时间戳转换、计算月份天数、获取星期等常用日期处理方法
*/
public class DateUtils {
public DateUtils() {
// 默认构造函数(空实现)
}
/**
* 将 Date 对象格式化为指定格式的字符串
* @param date 日期对象
* @param pattern 格式字符串(如 "yyyy-MM-dd")
* @return 格式化后的字符串
*/
public static String DateToString(Date date, String pattern) {
String strDateTime = null;
SimpleDateFormat formater = new SimpleDateFormat(pattern);
strDateTime = date == null ? null : formater.format(date);
return strDateTime;
}
/**
* 将 Date 对象格式化为默认格式 "yyyy-MM-dd"
* @param date 日期对象
* @return 格式化后的字符串
*/
public static String DateToString(Date date) {
String _pattern = "yyyy-MM-dd";
return date == null ? null : DateToString(date, _pattern);
}
/**
* 将 Date 对象格式化为带时间的默认格式 "yyyy-MM-dd HH:mm:ss"
* @param date 日期对象
* @return 格式化后的字符串
*/
public static String DateTimeToString(Date date) {
String _pattern = "yyyy-MM-dd HH:mm:ss";
return date == null ? null : DateToString(date, _pattern);
}
/**
* 将字符串格式的时间 "yyyy-MM-dd HH:mm:ss" 转换为毫秒级时间戳
* @param ts 时间字符串
* @return 返回对应的时间戳(毫秒)
* @throws Exception 解析失败时抛出异常
*/
public static long dateToStamp(String ts) throws Exception {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = simpleDateFormat.parse(ts);
return date.getTime();
}
public static List<String> generateDateList(String start, String end) {
List<String> dateList = new ArrayList<>();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate startDate = LocalDate.parse(start, formatter);
LocalDate endDate = LocalDate.parse(end, formatter);
for (LocalDate date = startDate; !date.isAfter(endDate); date = date.plusDays(1)) {
dateList.add(date.format(formatter));
}
return dateList;
}
/**
* 计算两个时间点之间的中间时刻(HH:mm 格式)
* 注意:不能跨零点使用
* @param start_ts 开始时间字符串(HH:mm)
* @param end_ts 结束时间字符串(HH:mm)
* @return 中间时间点字符串(HH:mm)
* @throws Exception 解析失败时抛出异常
*/
public static String getbtweetHm(String start_ts, String end_ts) throws Exception {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
Date date = simpleDateFormat.parse(start_ts);
Date date2 = simpleDateFormat.parse(end_ts);
long t = date.getTime() + ((date2.getTime() - date.getTime()) / 2);
String hm = DateToString(new Date(t), "HH:mm");
return hm;
}
/**
* 获取某个日期之后第 N 天的日期字符串
* @param datestr 基准日期字符串(yyyy-MM-dd)
* @param day 向后偏移的天数
* @return 偏移后的日期字符串
* @throws Exception 解析失败时抛出异常
*/
public static String afterDate(String datestr, long day) throws Exception {
Long d = dateToStamp2(datestr) + (day * 86400000); // 一天毫秒数:86400000
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String sb = format.format(d);
return sb;
}
/**
* 将字符串格式的日期 "yyyy-MM-dd" 转换为毫秒级时间戳
* @param ts 时间字符串
* @return 返回对应的时间戳(毫秒)
* @throws Exception 解析失败时抛出异常
*/
public static long dateToStamp2(String ts) throws Exception {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = simpleDateFormat.parse(ts);
return date.getTime();
}
/**
* 获取某个日期之前第 N 天的日期字符串
* @param datestr 基准日期字符串(yyyy-MM-dd)
* @param day 向前偏移的天数
* @return 偏移后的日期字符串
* @throws Exception 解析失败时抛出异常
*/
public static String beforeDate(String datestr, long day) throws Exception {
Long d = dateToStamp2(datestr) - (day * 86400000); // 一天毫秒数:86400000
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String sb = format.format(d);
return sb;
}
/**
* 获取指定日期是星期几
* @param date 日期对象
* @return 返回字符串表示的星期("1"=周一,..."7"=周日)
*/
public static String getWeekOfDate(Date date) {
String[] weekDays = new String[]{"7", "1", "2", "3", "4", "5", "6"}; // 星期数组(从周日开始)
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // DAY_OF_WEEK 是从周日开始的
if (w < 0) {
w = 0;
}
return weekDays[w];
}
/**
* 获取某个月份的总天数
* @param date 日期对象
* @return 返回该月的总天数
*/
public static int getDaysOfMonth(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.getActualMaximum(Calendar.DAY_OF_MONTH); // 获取该月最大天数
}
/**
* 获取当前时间的图片命名格式字符串(yyyyMMddHHmmss)
* @return 当前时间字符串
*/
public static String getImgName() {
String fcts = DateToString(new Date(), "yyyyMMddHHmmss");
return fcts;
}
/**
* 将字符串格式的月份 "yyyy-MM" 转换为 Date 对象
* @param m 月份字符串
* @return 返回对应的 Date 对象
* @throws Exception 解析失败时抛出异常
*/
public static Date StringToMonth(String m) throws Exception {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM");
return formatter.parse(m);
}
/**
* 将字符串格式的日期 "yyyy-MM-dd" 转换为 Date 对象
* @param ts 日期字符串
* @return 返回对应的 Date 对象
* @throws Exception 解析失败时抛出异常
*/
public static Date StringToDate(String ts) throws Exception {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
return formatter.parse(ts);
}
/**
* 计算指定年月的总天数(静态方法)
* 支持闰年判断
* @param year_month 年月字符串(如 "2024-04")
* @return 返回该月总天数
*/
public static Integer cont_days(String year_month) {
Integer year = Integer.parseInt(year_month.split("-")[0]);
String month = year_month.split("-")[1];
int days = 1;
if (month.equals("02")) { // 判断是否为闰年
if (year % 4 == 0 && year % 100 != 0) {
days = 29;
} else {
days = 28;
}
} else if (month.equals("01") || month.equals("03") || month.equals("05") ||
month.equals("07") || month.equals("08") || month.equals("10") ||
month.equals("12")) {
days = 31; // 31 天的月份
} else {
days = 30; // 30 天的月份
}
return days;
}
}
好博客就要一起分享哦!分享海报
此处可发布评论
评论(0)展开评论
暂无评论,快来写一下吧
展开评论
他的专栏
他感兴趣的技术






java
vue
springboot
Mysql
ssm
小程序
uniapp
js和jquery