业务开发中,若要求获取下一个工作日或上一个工作日,则可使用以下Java类。
package com.cmit.kapok.system.utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.time.DayOfWeek;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
/**
* @Title: 计算时间周期之间工作日的时间
* #Description: CalcWorkDaysUtil
* #Date: 2022/6/2 14:42
*/
public class CalcWorkDaysUtil {
private Logger logger = LoggerFactory.getLogger(CalcWorkDaysUtil.class);
// 定义2个List用于存储特殊的工作日和非工作日
// 特别的工作日(周六,周日上班的日期集合)
private static List<String> SPECIAL_WORK_DAYS = new ArrayList<>();
// 特别的休息日(周一到周五休息的日期集合)
private static List<String> SPECIAL_OFF_DAYS = new ArrayList<>();
// 在这里我就使用静态将2022年所有的节假日存到相应的list中
// 大多是都是从数据库中获取的(这里就这样演示,大家后期可以从数据库获取)
static {
// 特殊的工作日(周六、周日上班的日期集合)
SPECIAL_WORK_DAYS.add("2022-01-29");
SPECIAL_WORK_DAYS.add("2022-01-30");
SPECIAL_WORK_DAYS.add("2022-04-02");
SPECIAL_WORK_DAYS.add("2022-04-24");
SPECIAL_WORK_DAYS.add("2022-05-07");
SPECIAL_WORK_DAYS.add("2022-10-08");
SPECIAL_WORK_DAYS.add("2022-10-09");
// 特殊的休息日(周一到周五休息的日期集合)
SPECIAL_OFF_DAYS.add("2022-01-01");
SPECIAL_OFF_DAYS.add("2022-01-02");
SPECIAL_OFF_DAYS.add("2022-01-03");
SPECIAL_OFF_DAYS.add("2022-01-31");
SPECIAL_OFF_DAYS.add("2022-02-01");
SPECIAL_OFF_DAYS.add("2022-02-02");
SPECIAL_OFF_DAYS.add("2022-02-03");
SPECIAL_OFF_DAYS.add("2022-02-04");
SPECIAL_OFF_DAYS.add("2022-02-05");
SPECIAL_OFF_DAYS.add("2022-02-06");
SPECIAL_OFF_DAYS.add("2022-04-03");
SPECIAL_OFF_DAYS.add("2022-04-04");
SPECIAL_OFF_DAYS.add("2022-04-05");
SPECIAL_OFF_DAYS.add("2022-04-30");
SPECIAL_OFF_DAYS.add("2022-05-01");
SPECIAL_OFF_DAYS.add("2022-05-02");
SPECIAL_OFF_DAYS.add("2022-05-03");
SPECIAL_OFF_DAYS.add("2022-05-04");
SPECIAL_OFF_DAYS.add("2022-06-03");
SPECIAL_OFF_DAYS.add("2022-06-04");
SPECIAL_OFF_DAYS.add("2022-06-05");
SPECIAL_OFF_DAYS.add("2022-09-10");
SPECIAL_OFF_DAYS.add("2022-09-11");
SPECIAL_OFF_DAYS.add("2022-09-12");
SPECIAL_OFF_DAYS.add("2022-10-01");
SPECIAL_OFF_DAYS.add("2022-10-01");
SPECIAL_OFF_DAYS.add("2022-10-02");
SPECIAL_OFF_DAYS.add("2022-10-03");
SPECIAL_OFF_DAYS.add("2022-10-04");
SPECIAL_OFF_DAYS.add("2022-10-05");
SPECIAL_OFF_DAYS.add("2022-10-06");
SPECIAL_OFF_DAYS.add("2022-10-07");
}
// 日期格式化方法(非线程安全的,可根据情况在方法内使用,或使用DateTimeFormatter)
private static DateTimeFormatter SDF = DateTimeFormatter.ofPattern("yyyy-MM-dd");
private static DateTimeFormatter SDFS = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
public static LocalDateTime nextWorkDay(LocalDateTime dateTime){
LocalDateTime modifiedDate = dateTime.plusDays(1);
DayOfWeek week = modifiedDate.getDayOfWeek();
String dateStr = modifiedDate.format(SDF);
//如果是周末,但是是特别工作日
if((week.equals(DayOfWeek.SATURDAY) || week.equals(DayOfWeek.SUNDAY)) && SPECIAL_WORK_DAYS.contains(dateStr)){
return modifiedDate;
}else if(!week.equals(DayOfWeek.SATURDAY) && !week.equals(DayOfWeek.SUNDAY) && !SPECIAL_OFF_DAYS.contains(dateStr)){
return modifiedDate;
}else{
return nextWorkDay(modifiedDate);
}
}
public static LocalDateTime nextWorkDay(LocalDateTime dateTime,Integer num){
if(num<1){
return dateTime;
}
while(num>0){
dateTime = nextWorkDay(dateTime);
num--;
}
return dateTime ;
}
public static LocalDateTime lastWorkDay(LocalDateTime dateTime){
LocalDateTime modifiedDate = dateTime.minusDays(1);
DayOfWeek week = modifiedDate.getDayOfWeek();
String dateStr = modifiedDate.format(SDF);
//如果是周末,但是是特别工作日
if((week.equals(DayOfWeek.SATURDAY) || week.equals(DayOfWeek.SUNDAY)) && SPECIAL_WORK_DAYS.contains(dateStr)){
return modifiedDate;
}else if(!week.equals(DayOfWeek.SATURDAY) && !week.equals(DayOfWeek.SUNDAY) && !SPECIAL_OFF_DAYS.contains(dateStr)){
return modifiedDate;
}else{
return lastWorkDay(modifiedDate);
}
}
public static LocalDateTime lastWorkDay(LocalDateTime dateTime,Integer num){
if(num<1){
return dateTime;
}
while(num>0){
dateTime = lastWorkDay(dateTime);
num--;
}
return dateTime ;
}
public static void main(String[] args) {
LocalDateTime input = LocalDateTime.of(2022,11,28,12,12);
LocalDateTime dateTime = lastWorkDay(input);
System.out.println(dateTime);
}
}