获取指定n个工作日后的日期工具类

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class WeekdaysUtils {
public static List<String> list = new ArrayList<>();//节假日数据源
public static List<String> weekDayList = new ArrayList<>();//补班数据源

static {
//数据源需扩充,每年更新
/**
* 2021年假期及补办数据源
*/
list.add("2021-01-01");
list.add("2021-01-02");
list.add("2021-01-03");
list.add("2021-02-11");
list.add("2021-02-12");
list.add("2021-02-13");
list.add("2021-02-14");
list.add("2021-02-15");
list.add("2021-02-16");
list.add("2021-02-17");
list.add("2021-04-03");
list.add("2021-04-04");
list.add("2021-04-05");
list.add("2021-05-01");
list.add("2021-05-02");
list.add("2021-05-03");
list.add("2021-05-04");
list.add("2021-05-05");
list.add("2021-06-12");
list.add("2021-06-13");
list.add("2021-06-14");
list.add("2021-09-19");
list.add("2021-09-20");
list.add("2021-09-21");
list.add("2021-10-01");
list.add("2021-10-02");
list.add("2021-10-03");
list.add("2021-10-04");
list.add("2021-10-05");
list.add("2021-10-06");
list.add("2021-10-07");
weekDayList.add("2021-02-07");
weekDayList.add("2021-02-20");
weekDayList.add("2021-04-25");
weekDayList.add("2021-05-08");
weekDayList.add("2021-09-18");
weekDayList.add("2021-09-26");
weekDayList.add("2021-10-09");
/**
* 2022年假期及补办数据源
*/
//元旦节
list.add("2022-01-01");
list.add("2022-01-02");
list.add("2022-01-03");
//除夕
list.add("2022-01-31");
//春节
list.add("2022-02-01");
list.add("2022-02-02");
list.add("2022-02-03");
list.add("2022-02-04");
list.add("2022-02-05");
list.add("2022-02-06");
//清明节
list.add("2022-04-03");
list.add("2022-04-04");
list.add("2022-04-05");
//劳动节
list.add("2022-05-01");
list.add("2022-05-02");
list.add("2022-05-03");
list.add("2022-05-04");
//端午节
list.add("2022-06-03");
list.add("2022-06-04");
list.add("2022-06-05");
//国庆节
list.add("2022-10-01");
list.add("2022-10-02");
list.add("2022-10-03");
list.add("2022-10-04");
list.add("2022-10-05");
list.add("2022-10-06");
list.add("2022-10-07");
weekDayList.add("2022-01-29");
weekDayList.add("2022-01-30");
weekDayList.add("2022-04-02");
weekDayList.add("2022-04-24");
weekDayList.add("2022-05-07");
weekDayList.add("2022-10-08");
weekDayList.add("2022-10-09");
/**
* 2023年假期及补办数据源
*/
//元旦节
list.add("2022-12-31");
list.add("2023-01-01");
list.add("2023-01-02");
//除夕
list.add("2023-01-21");
//春节
list.add("2023-01-22");
list.add("2023-01-23");
list.add("2023-01-24");
list.add("2023-01-25");
list.add("2023-01-26");
list.add("2023-01-27");
//清明节
list.add("2023-04-05");
//劳动节
list.add("2023-04-29");
list.add("2023-04-30");
list.add("2023-05-01");
list.add("2023-05-02");
list.add("2023-05-03");
//端午节
list.add("2023-06-22");
list.add("2023-06-23");
list.add("2023-06-24");
//国庆节
list.add("2023-09-29");
list.add("2023-09-30");
list.add("2023-10-01");
list.add("2023-10-02");
list.add("2023-10-03");
list.add("2023-10-04");
list.add("2023-10-05");
list.add("2023-10-06");
weekDayList.add("2023-01-28");
weekDayList.add("2023-01-29");
weekDayList.add("2023-04-23");
weekDayList.add("2023-05-06");
weekDayList.add("2023-06-25");
weekDayList.add("2023-10-07");
weekDayList.add("2023-10-08");
}

private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

/**
* 用于判断n个工作日(排除节假日、周六日包含节后补班数据)后的日期
*
* @param list 节假日数据源
* @param weekDayList 节后补班数据源
* @param today 计算开始时间
* @param num 多少个工作日 根据需要自行安排
* @throws ParseException
*/
public static Date getScheduleActiveDate(List<String> list, List<String> weekDayList, Date today, int num) {
try {
String today1 = WeekdaysUtils.parseDate(today, "yyyy-MM-dd");
Date tomorrow = null;
int delay = 1;
while (delay <= num) {
tomorrow = getTomorrow(today);
//当前日期+1即tomorrow,判断是否是节假日,同时要判断是否是周末,都不是则将scheduleActiveDate日期+1,直到循环num次即可------不是节假日不是周末并且不是补班
if ((!isWeekend(sdf.format(tomorrow)) && !isHoliday(sdf.format(tomorrow), list)) || isWorkWeekDay(sdf.format(tomorrow), weekDayList)) {
if (isWorkWeekDay(sdf.format(tomorrow), weekDayList)) {
System.out.println(sdf.format(tomorrow) + "::是节假日调休补班");
} else {
System.out.println(sdf.format(tomorrow) + "::是正常工作日");
}
delay++;
today = tomorrow;
} else if (isHoliday(sdf.format(tomorrow), list)) {
// tomorrow = getTomorrow(tomorrow);
today = tomorrow;
System.out.println(sdf.format(tomorrow) + "::是节假日");
} else if (isWeekend(sdf.format(tomorrow))) {//是周六日并且不是节后补班
if (!isWorkWeekDay(sdf.format(tomorrow), weekDayList)) {
today = tomorrow;
System.out.println(sdf.format(tomorrow) + "::是休息日");
}
}
}
System.out.println(today1 + "后" + num + "个工作日后,日期为::" + sdf.format(today));
return today;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

/**
* 获取明天的日期
*
* @author Herbert
*/
public static Date getTomorrow(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_MONTH, +1);
date = calendar.getTime();
return date;
}

/**
* 判断是否是weekend
*/
public static boolean isWeekend(String sdate) throws ParseException {
Date date = sdf.parse(sdate);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
return true;
} else {
return false;
}
}

/**
* 判断是否是holiday
*/
public static boolean isHoliday(String sdate, List<String> list) throws ParseException {
if (list.size() > 0) {
for (int i = 0; i < list.size(); i++) {
if (sdate.equals(list.get(i))) {
return true;
}
}
}
return false;
}

/**
* 判断是否是补班
*/
public static boolean isWorkWeekDay(String sdate, List<String> list) throws ParseException {
if (list.size() > 0) {
for (int i = 0; i < list.size(); i++) {
if (sdate.equals(list.get(i))) {
return true;
}
}
}
return false;
}

/**
* 把日期格式化成字符串
*/
public static String parseDate(Date date, String format) {
SimpleDateFormat formater = new SimpleDateFormat(format);
String dateString;
dateString = formater.format(date);
return dateString;
}

/**
* (differ / dayM) 这里拿到被舍弃的整数,整数是几,就代表相隔几天,一天24小时,那就整数乘以24即可。
*
* @param endDate
* @param nowDate
* @return
*/
public static String getDatePoor(Date endDate, Date nowDate) {
long nd = 1000 * 24 * 60 * 60;//每天毫秒数
long nh = 1000 * 60 * 60;//每小时毫秒数
long nm = 1000 * 60;//每分钟毫秒数
long diff = endDate.getTime() - nowDate.getTime(); // 获得两个时间的毫秒时间差异
long day = diff / nd; // 计算差多少天
long hour = diff % nd / nh; // 计算差多少小时
long min = diff % nd % nh / nm; // 计算差多少分钟
return hour + "小时" + min + "分钟";
}

/**
* 查询两个时间之间有多少个工作日
*
* @param list 节假日数据源
* @param weekDayList 节后补班数据源
* @param endDate 结束时间
* @param nowDate 开始时间
* @return
*/
public static Map<String, Integer> getDateWeekdayNum(List<String> list, List<String> weekDayList, Date endDate, Date nowDate) {
Map<String, Integer> resultMap = new HashMap<>();
try {
String endDateStr = WeekdaysUtils.parseDate(endDate, "yyyy-MM-dd");
String nowDateStr = WeekdaysUtils.parseDate(nowDate, "yyyy-MM-dd");
//相等则是同一天
if (endDateStr.equals(nowDateStr)) {
resultMap.put("weekday", 0);
resultMap.put("holiday", 0);
return resultMap;
}
Date tomorrow = null;
int weekday = 0;
int holiday = 0;
while (true) {
// + 1
tomorrow = getTomorrow(nowDate);
String str = sdf.format(tomorrow);
//当前日期+1即tomorrow,判断是否是节假日,同时要判断是否是周末,都不是则将scheduleActiveDate日期+1,直到循环num次即可------不是节假日不是周末并且不是补班
if ((!isWeekend(sdf.format(tomorrow)) && !isHoliday(sdf.format(tomorrow), list)) || isWorkWeekDay(sdf.format(tomorrow), weekDayList)) {
if (isWorkWeekDay(sdf.format(tomorrow), weekDayList)) {
System.out.println(sdf.format(tomorrow) + "::是节假日调休补班");
} else {
System.out.println(sdf.format(tomorrow) + "::是正常工作日");
}
//工作日/调班
++weekday;
nowDate = tomorrow;
} else {
//休假日
++holiday;
nowDate = tomorrow;
}
if (endDateStr.equals(sdf.format(tomorrow))) {
break;
}
}
resultMap.put("weekday", weekday);
resultMap.put("holiday", holiday);
return resultMap;
} catch (Exception e) {
e.printStackTrace();
}
return resultMap;
}
}
posted @ 2023-02-01 16:28  懂得归零  阅读(94)  评论(0编辑  收藏  举报