/**
* 中国法定节假日期
*/
public static final String IS_HOLIDAY = "01-01,01-02,01-03,02-09,02-10,02-11,02-12,02-13,02-14,"
+ "02-15,04-04,04-05,04-06,04-29,04-30,05-01,06-10,06-11,"
+ "06-12,09-19,09-20,09-21,10-01,10-02,10-03,10-04,10-05,10-06,10-07";
/**
* 节假前后加班日期
*/
public static final String OVER_DAY = "01-05,01-06,02-16,02-17,04-07,04-17,04-28,06-08,06-09,09-18,09-26,10-09";
/**
* 获取今天 workingDay 个工作日之后的日期
* @param calendar 当前时间的日历对象 [ 如果日历为null,默认获取当前日期 ]
* @param simpleDateFormat 日期格式转换器
* @param workingDay 获取到少个工作日之后的排班 [ 默认 3 天之后 ]
* @return String 最终的日期
*/
public static String getStartDate(ThreadLocal<SimpleDateFormat> simpleDateFormat,Calendar calendar,Integer workingDay) throws ParseException {
if(simpleDateFormat == null){
simpleDateFormat = new ThreadLocal<>();
simpleDateFormat.set(new SimpleDateFormat("yyyy-MM-dd"));
}
if(calendar == null){
String newDate = simpleDateFormat.get().format(new Date());
Date parse1 = simpleDateFormat.get().parse(newDate);
calendar = Calendar.getInstance();
calendar.setTime(parse1);
}
workingDay = workingDay == null ? 3 : workingDay;
SimpleDateFormat sdfDate = new SimpleDateFormat("MM-dd");
String nowDate;
int week;
boolean b = true;
// 循环处理日期
while (b) {
calendar.add(Calendar.DATE, 1);
nowDate = sdfDate.format(calendar.getTime());
week = calendar.get(Calendar.DAY_OF_WEEK) - 1;
boolean hasWorkingDay = IS_HOLIDAY.contains(nowDate) || week == 0 || week == 6;
//条件一 当前不是假期 /条件二:当前是国定加班日
if(!hasWorkingDay || OVER_DAY.contains(nowDate)){
workingDay --;
}
b = hasWorkingDay || workingDay >= 0;
}
// 记得remove掉 threadLocal
return simpleDateFormat.get().format(calendar.getTime());
}
ConnectUtil
public static final ThreadLocal<SimpleDateFormat> threadLocal = new ThreadLocal<>();
public static void main(String[] args) throws ParseException {
ConnectUtil.threadLocal.set(new SimpleDateFormat("yyyy-MM-dd"));
System.out.println(getStartDate( ConnectUtil.threadLocal, null, 3));
}