直接附上操作工具类源码:
package com.janwes;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.temporal.ChronoUnit;
import java.util.Date;
public class DateUtil {
public static LocalDate dateToLocalDate(Date date) {
return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
}
public static LocalDateTime dateToLocalDateTime(Date date) {
return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
}
public static ZonedDateTime dateToZonedDateTime(Date date) {
return date.toInstant().atZone(ZoneId.systemDefault());
}
public static Date toDate(LocalDate localDate) {
return Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
}
public static Date toDate(LocalDateTime localDateTime) {
return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
}
public static Date toDate(ZonedDateTime zonedDateTime) {
return Date.from(zonedDateTime.toInstant());
}
public static String dateFormat(Date date, String pattern) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
return simpleDateFormat.format(date);
}
public static Date strDateParseDate(String strDate, String pattern) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
try {
return simpleDateFormat.parse(strDate);
} catch (ParseException e) {
throw new IllegalArgumentException(String.format("parse strDate %s failure, maybe %s and %s is don't match", strDate, strDate, pattern));
}
}
public static Date plusDays(Date date, long offsetDay) {
final LocalDate localDate = DateUtil.dateToLocalDate(date);
final LocalDate plusLocalDate = localDate.plusDays(offsetDay);
return DateUtil.toDate(plusLocalDate);
}
public static LocalDate plusDays(LocalDate localDate, long offsetDay) {
return localDate.plusDays(offsetDay);
}
public static LocalDateTime plusDays(LocalDateTime localDateTime, long offsetDay) {
return localDateTime.plusDays(offsetDay);
}
public static LocalDate minusDays(LocalDate localDate, long offsetDay) {
return localDate.minusDays(offsetDay);
}
public static LocalDateTime minusDays(LocalDateTime localDateTime, long offsetDay) {
return localDateTime.minusDays(offsetDay);
}
public static Date minusDays(Date date, long offsetDay) {
final LocalDate localDate = DateUtil.dateToLocalDate(date);
final LocalDate minusLocalDate = localDate.minusDays(offsetDay);
return DateUtil.toDate(minusLocalDate);
}
public static Date plusMonths(Date date, long offsetMonths) {
final LocalDate localDate = DateUtil.dateToLocalDate(date);
final LocalDate plusLocalDate = localDate.plusMonths(offsetMonths);
return DateUtil.toDate(plusLocalDate);
}
public static LocalDate plusMonths(LocalDate localDate, long offsetMonths) {
return localDate.plusMonths(offsetMonths);
}
public static LocalDateTime plusMonths(LocalDateTime localDateTime, long offsetMonths) {
return localDateTime.plusMonths(offsetMonths);
}
public static Date minusMonths(Date date, long offsetMonths) {
final LocalDate localDate = DateUtil.dateToLocalDate(date);
final LocalDate plusLocalDate = localDate.minusMonths(offsetMonths);
return DateUtil.toDate(plusLocalDate);
}
public static LocalDate minusMonths(LocalDate localDate, long offsetMonths) {
return localDate.minusMonths(offsetMonths);
}
public static LocalDateTime minusMonths(LocalDateTime localDateTime, long offsetMonths) {
return localDateTime.minusMonths(offsetMonths);
}
public static long diffHours(Date startDate, Date endDate) {
// Date转成LocalDate
LocalDateTime startLocalDateTime = startDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
LocalDateTime endLocalDateTime= endDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
// 计算相差时数
return ChronoUnit.HOURS.between(startLocalDateTime, endLocalDateTime);
}
public static long diffHours(LocalDateTime startLocalDateTime, LocalDateTime endLocalDateTime) {
// 计算相差时数
return ChronoUnit.HOURS.between(startLocalDateTime, endLocalDateTime);
}
public static long diffDays(Date startDate, Date endDate) {
// Date转成LocalDate
LocalDate startLocalDate = startDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
LocalDate endLocalDate = endDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
// Math.abs(startLocalDate.toEpochDay() - endLocalDate.toEpochDay()); // 转换成绝对值(不出现负数)
//return startLocalDate.toEpochDay() - endLocalDate.toEpochDay();
// 计算相差天数
return ChronoUnit.DAYS.between(startLocalDate, endLocalDate);
}
public static long diffDays(LocalDate startLocalDate, LocalDate endLocalDate) {
// 计算相差天数
return ChronoUnit.DAYS.between(startLocalDate, endLocalDate);
}
public static long diffDays(LocalDateTime startLocalDateTime, LocalDateTime endLocalDateTime) {
// 两种方式计算相差天数,精确至时分秒计算,与LocalDate有实质性的区别,LocalDate忽略时分秒的差异
Duration duration = Duration.between(startLocalDateTime, endLocalDateTime);
return duration.toDays();
// 计算相差天数
//return ChronoUnit.DAYS.between(startLocalDateTime, endLocalDateTime);
}
public static long diffMonths(Date startDate, Date endDate) {
// Date转成LocalDate
LocalDate startLocalDate = startDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
LocalDate endLocalDate = endDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
// 计算相差月数
return ChronoUnit.MONTHS.between(startLocalDate, endLocalDate);
}
public static long diffMonths(LocalDate startLocalDate, LocalDate endLocalDate) {
// 计算相差月数
return ChronoUnit.MONTHS.between(startLocalDate, endLocalDate);
}
public static long diffMonths(LocalDateTime startLocalDateTime, LocalDateTime endLocalDateTime) {
// 计算相差月数
return ChronoUnit.MONTHS.between(startLocalDateTime, endLocalDateTime);
}
}
敬请期待,后续持续更新中…



