目录
一、计算俩个日期之间相差多少天
// 假设有两个Date对象
Date date1 = new Date(); // 当前时间
Date date2 = new Date(); // 可以设置为其他时间
//将Date 转为 LocalDate
//推荐使用以下这种方法转
LocalDate localDateA = date1.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
LocalDate localDateB = date2.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
// 计算天数差
long daysBetween = ChronoUnit.DAYS.between(localDate1, localDate2);
System.out.println("Days between: " + daysBetween);
先把 Date 转为 LocalDate ,接着调用 JDK8 引入的 ChronoUnit 工具类计算相差的天数
使用以上的方法把 Date 转换为 LocalDate 是因为这样可以指定时区
二、计算俩个日期之间相差多少天(去除周末)
Date dateA = new Date();
Date dateB = new Date();
//将Date 转为 LocalDate
//推荐使用以下方法进行转换
LocalDate localDateA = dateA.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
LocalDate localDateB = dateB.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
long workdays = countWorkdaysBetween(localDateA, localDateB);
System.out.println("工作日数量: " + workdays);
private static long countWorkdaysBetween(LocalDate startDate, LocalDate endDate) {
long workdays = 0;
LocalDate tempDate = startDate;
while (!tempDate.isAfter(endDate)) {
DayOfWeek dayOfWeek = tempDate.getDayOfWeek();
if (dayOfWeek != DayOfWeek.SATURDAY && dayOfWeek != DayOfWeek.SUNDAY) {
workdays++;
}
tempDate = tempDate.plusDays(1);
}
return workdays;
}
调用 LocalDate 类下的 getDayOfWeek 方法获取当前日期到底是周几,在获取相差天数的时候过滤掉周末
三、计算俩个日期之间相差多少天(去除周末和法定节假日)
// 假设这是你的两个日期
Date dateA = new Date();
Date dateB = new Date();
// 转换为 LocalDate
LocalDate localDateA = dateA.toInstant().atZone(ZoneId.systemDefault()).toLocalDat
LocalDate localDateB = dateB.toInstant().atZone(ZoneId.systemDefault()).toLocalDat
// 假设这些是你的法定假日
List<LocalDate> holidays = new ArrayList<>(Arrays.asList(
LocalDate.of(2024, 1, 1), // New Year's Day
LocalDate.of(2024, 12, 25) // Christmas
// 添加更多假日...
));
// 计算工作日
int businessDays = countBusinessDays(localDateA, localDateB, holidays);
System.out.println("Business days between the two dates: " + businessDays);
//计算相差人数具体方法
private static int countBusinessDays(LocalDate start, LocalDate end, List<LocalDate> holidays) {
int businessDays = 0;
for (LocalDate date = start; !date.isAfter(end); date = date.plusDays(1)) {
if (!isWeekend(date) && !isHoliday(date, holidays)) {
businessDays++;
}
}
return businessDays;
}
//判断是否是周末
private static boolean isWeekend(LocalDate date) {
return date.getDayOfWeek() == DayOfWeek.SATURDAY || date.getDayOfWeek() == DayOfWeek.SUNDAY;
}
//判断是否是节假日
private static boolean isHoliday(LocalDate date, List<LocalDate> holidays) {
return holidays.contains(date);
}
这里我们是写死的节假日,但是如果我们想要动态获取的话可以调用第三方 api 获取,具体可以百度参考其它文章
以上适用于 JDK8 ,如果我们的项目使用的是 JDK7 怎么办呢,让我们接着往下看
四、JDK7计算俩个日期之间相差多少天
// 假设这是你的两个日期
Date dateA = new Date(); // 替换为具体的日期
Date dateB = new Date(); // 替换为具体的日期
// 计算天数差
long daysBetween = calculateDaysBetween(dateA, dateB);
System.out.println("Days between the two dates: " + daysBetween);
private static long calculateDaysBetween(Date dateA, Date dateB) {
Calendar calendarA = Calendar.getInstance();
calendarA.setTime(dateA);
Calendar calendarB = Calendar.getInstance();
calendarB.setTime(dateB);
// 确保 dateA 不晚于 dateB
if (calendarA.after(calendarB)) {
// 交换日期
Calendar temp = calendarA;
calendarA = calendarB;
calendarB = temp;
}
// 设置时间为一天的开始
calendarA.set(Calendar.HOUR_OF_DAY, 0);
calendarA.set(Calendar.MINUTE, 0);
calendarA.set(Calendar.SECOND, 0);
calendarA.set(Calendar.MILLISECOND, 0);
calendarB.set(Calendar.HOUR_OF_DAY, 0);
calendarB.set(Calendar.MINUTE, 0);
calendarB.set(Calendar.SECOND, 0);
calendarB.set(Calendar.MILLISECOND, 0);
// 计算时间差
long diffInMillis = calendarB.getTimeInMillis() - calendarA.getTimeInMillis();
// 转换为天数
long diffInDays = diffInMillis / (1000 * 60 * 60 * 24);
return diffInDays;
}
使用 Calendar 对象的 getTimeInMillis 方法获取它们的毫秒值之后转换为天数