fix:项目bug
This commit is contained in:
232
common/src/main/java/com/sdm/common/utils/DateUtils.java
Normal file
232
common/src/main/java/com/sdm/common/utils/DateUtils.java
Normal file
@@ -0,0 +1,232 @@
|
||||
package com.sdm.common.utils;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.*;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.temporal.TemporalAdjusters;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 日期时间工具类
|
||||
*/
|
||||
public class DateUtils {
|
||||
|
||||
// 常用日期时间格式
|
||||
public static final String PATTERN_DEFAULT = "yyyy-MM-dd HH:mm:ss";
|
||||
public static final String PATTERN_DATE = "yyyy-MM-dd";
|
||||
public static final String PATTERN_TIME = "HH:mm:ss";
|
||||
public static final String PATTERN_SLASH = "yyyy/MM/dd";
|
||||
public static final String PATTERN_CHINESE = "yyyy年MM月dd日";
|
||||
public static final String PATTERN_COMPACT = "yyyyMMddHHmmss";
|
||||
public static final String PATTERN_COMPACT_DATE = "yyyyMMdd";
|
||||
|
||||
private DateUtils() {
|
||||
throw new IllegalStateException("Utility class");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前日期时间字符串
|
||||
*/
|
||||
public static String getCurrentDateTime() {
|
||||
return LocalDateTime.now().format(DateTimeFormatter.ofPattern(PATTERN_DEFAULT));
|
||||
}
|
||||
|
||||
/**
|
||||
* 日期转字符串
|
||||
*/
|
||||
public static String format(Date date, String pattern) {
|
||||
if (date == null) {
|
||||
return null;
|
||||
}
|
||||
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
|
||||
return sdf.format(date);
|
||||
}
|
||||
|
||||
/**
|
||||
* 日期转字符串(默认格式)
|
||||
*/
|
||||
public static String format(Date date) {
|
||||
return format(date, PATTERN_DEFAULT);
|
||||
}
|
||||
|
||||
/**
|
||||
* 字符串转日期
|
||||
*/
|
||||
public static Date parse(String dateStr, String pattern) {
|
||||
if (dateStr == null || dateStr.trim().isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
|
||||
return sdf.parse(dateStr);
|
||||
} catch (ParseException e) {
|
||||
throw new IllegalArgumentException("日期格式错误: " + dateStr, e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* LocalDateTime 转 Date
|
||||
*/
|
||||
public static Date toDate(LocalDateTime localDateTime) {
|
||||
if (localDateTime == null) {
|
||||
return null;
|
||||
}
|
||||
return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
|
||||
}
|
||||
|
||||
/**
|
||||
* LocalDate 转 Date
|
||||
*/
|
||||
public static Date toDate(LocalDate localDate) {
|
||||
if (localDate == null) {
|
||||
return null;
|
||||
}
|
||||
return Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
|
||||
}
|
||||
|
||||
/**
|
||||
* Date 转 LocalDateTime
|
||||
*/
|
||||
public static LocalDateTime toLocalDateTime(Date date) {
|
||||
if (date == null) {
|
||||
return null;
|
||||
}
|
||||
return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
|
||||
}
|
||||
|
||||
/**
|
||||
* Date 转 LocalDate
|
||||
*/
|
||||
public static LocalDate toLocalDate(Date date) {
|
||||
if (date == null) {
|
||||
return null;
|
||||
}
|
||||
return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定日期的开始时间(00:00:00)
|
||||
*/
|
||||
public static Date getStartOfDay(Date date) {
|
||||
if (date == null) {
|
||||
return null;
|
||||
}
|
||||
LocalDateTime localDateTime = toLocalDateTime(date)
|
||||
.toLocalDate()
|
||||
.atStartOfDay();
|
||||
return toDate(localDateTime);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定日期的结束时间(23:59:59)
|
||||
*/
|
||||
public static Date getEndOfDay(Date date) {
|
||||
if (date == null) {
|
||||
return null;
|
||||
}
|
||||
LocalDateTime localDateTime = toLocalDateTime(date)
|
||||
.toLocalDate()
|
||||
.atTime(LocalTime.MAX);
|
||||
return toDate(localDateTime);
|
||||
}
|
||||
|
||||
/**
|
||||
* 日期加减(天数)
|
||||
*/
|
||||
public static Date addDays(Date date, int days) {
|
||||
if (date == null) {
|
||||
return null;
|
||||
}
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.setTime(date);
|
||||
calendar.add(Calendar.DAY_OF_MONTH, days);
|
||||
return calendar.getTime();
|
||||
}
|
||||
|
||||
/**
|
||||
* 日期加减(月数)
|
||||
*/
|
||||
public static Date addMonths(Date date, int months) {
|
||||
if (date == null) {
|
||||
return null;
|
||||
}
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.setTime(date);
|
||||
calendar.add(Calendar.MONTH, months);
|
||||
return calendar.getTime();
|
||||
}
|
||||
|
||||
/**
|
||||
* 日期加减(年数)
|
||||
*/
|
||||
public static Date addYears(Date date, int years) {
|
||||
if (date == null) {
|
||||
return null;
|
||||
}
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.setTime(date);
|
||||
calendar.add(Calendar.YEAR, years);
|
||||
return calendar.getTime();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取两个日期之间的天数差
|
||||
*/
|
||||
public static long daysBetween(Date startDate, Date endDate) {
|
||||
if (startDate == null || endDate == null) {
|
||||
throw new IllegalArgumentException("日期不能为空");
|
||||
}
|
||||
LocalDate start = toLocalDate(startDate);
|
||||
LocalDate end = toLocalDate(endDate);
|
||||
return Math.abs(start.until(end).getDays());
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否为同一天
|
||||
*/
|
||||
public static boolean isSameDay(Date date1, Date date2) {
|
||||
if (date1 == null || date2 == null) {
|
||||
return false;
|
||||
}
|
||||
LocalDate localDate1 = toLocalDate(date1);
|
||||
LocalDate localDate2 = toLocalDate(date2);
|
||||
return localDate1.isEqual(localDate2);
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断日期是否在指定范围内
|
||||
*/
|
||||
public static boolean isBetween(Date date, Date startDate, Date endDate) {
|
||||
if (date == null || startDate == null || endDate == null) {
|
||||
return false;
|
||||
}
|
||||
return !date.before(startDate) && !date.after(endDate);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取月份的第一天
|
||||
*/
|
||||
public static Date getFirstDayOfMonth(Date date) {
|
||||
if (date == null) {
|
||||
return null;
|
||||
}
|
||||
LocalDate localDate = toLocalDate(date)
|
||||
.with(TemporalAdjusters.firstDayOfMonth());
|
||||
return toDate(localDate);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取月份的最后一天
|
||||
*/
|
||||
public static Date getLastDayOfMonth(Date date) {
|
||||
if (date == null) {
|
||||
return null;
|
||||
}
|
||||
LocalDate localDate = toLocalDate(date)
|
||||
.with(TemporalAdjusters.lastDayOfMonth());
|
||||
return toDate(localDate);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user