1、LocalDate方法
实际应用:
//将date日期类型转化为LocalDate数据类型, result.getCreateTime()为date参数
LocalDate startDate = result.getCreateTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
//获取当前日期
LocalDateTime endDate = LocalDateTime.now();
//startDate.until(endDate, ChronoUnit.DAYS))求起止时间的天数
result.setDays(String.valueOf(startDate.until(endDate, ChronoUnit.DAYS)));
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
//将String转化为LocalDate类型
LocalDate parse = LocalDate.parse("2022-07-26", formatter);
//将Date类型转化为LocalDate类型
LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
2、DateUtils方法
获取两个日期之间的天数差
public static int getDaysGapOfDates(Date startDate, Date endDate) {
long ONE_DAY_MILLS = 3600000 * 24;
int date = 0;
if (startDate != null && endDate != null) {
date = (int) ((endDate.getTime() - startDate.getTime()) / ONE_DAY_MILLS);
}
return date;
}