1.说明:
获取n个工作日后的日期(包含法定节假日、双休日、节后补班),需要在数据库手动入库当年的法定节假日及节假日调休补班。(也可写在配置文件中读取)
2.工具类代码
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.List; /** * 用于查询n个工作日(包含法定节假日、双休日、调休)后的日期 * @author ywh * @version 创建时间:2019年4月24日 下午5:50:29 */ public class HolidayUtils { private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); /** * 用于判断n个工作日(排除节假日、周六日包含节后补班数据)后的日期 * * @author ywh * @version 创建时间:2019年4月24日 上午11:24:58 * @param list 节假日数据源 * @param weekDayList 节后补班数据源 * @param today 计算开始时间 * @param num 多少个工作日 根据需要自行安排 * @return * @throws ParseException */ @SuppressWarnings("deprecation") public static Date getScheduleActiveDate(List<String> list,List<String> weekDayList, Date today,int num) throws ParseException { String today1 = DateUtils.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; } /** * 获取明天的日期 * * @author ywh * @version 创建时间:2019年4月23日 下午5:18:44 */ 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 * * @author ywh * @version 创建时间:2019年4月23日 下午5:19:27 */ 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 * * @author ywh * @version 创建时间:2019年4月23日 下午5:19:42 */ 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; } /** * 判断是否是补班 * * @author ywh * @version 创建时间:2019年4月23日 下午5:19:54 */ 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; } /** * 把日期格式化成字符串 * @param date * @param format 例: yyyy-MM-dd * @return */ public static String parseDate(Date date, String format) { SimpleDateFormat formater = new SimpleDateFormat(format); String dateString; dateString = formater.format(date); return dateString; } }
3.测试调用
public String holiday(String date){ Date today1 = new Date(); int num = 10;// List<Holiday> holidayList = holidayService.queryAllHoliday("0");//查询出法定节假日list List<Holiday> weekDayList =holidayService.queryAllHoliday("1");//查询节假日调休补班list List hoList = new ArrayList();//节假日数据源 List weList = new ArrayList();//节后节前数据源 for (Holiday holiday : holidayList) { hoList.add(holiday.getDate()); } for (Holiday holiday : weekDayList) { weList.add(holiday.getDate()); } try { Date date111 = HolidayUtils.getScheduleActiveDate(hoList,weList,today1,num); System.out.println(HolidayUtils.parseDate(date111, "yyyy-MM-dd")); } catch (ParseException e) { e.printStackTrace(); } return ""; }
4.sql脚本
DROP TABLE IF EXISTS `holiday`; CREATE TABLE `holiday` ( `GUID` varchar(36) COLLATE utf8_bin NOT NULL, `DATE` varchar(10) COLLATE utf8_bin DEFAULT NULL, `TYPE` varchar(1) COLLATE utf8_bin DEFAULT NULL COMMENT '0:法定节假日;1 节假日调休补班(如某个节假日前后周六日上班的情况)', PRIMARY KEY (`GUID`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='此表需每年根据法定节假日具体时间更新一次'; -- ---------------------------- -- Records of holiday -- ---------------------------- INSERT INTO `holiday` VALUES ('1', '2019-05-01', '0'); INSERT INTO `holiday` VALUES ('10', '2019-09-15', '0'); INSERT INTO `holiday` VALUES ('11', '2019-10-01', '0'); INSERT INTO `holiday` VALUES ('12', '2019-10-02', '0'); INSERT INTO `holiday` VALUES ('13', '2019-10-03', '0'); INSERT INTO `holiday` VALUES ('14', '2019-10-04', '0'); INSERT INTO `holiday` VALUES ('15', '2019-10-05', '0'); INSERT INTO `holiday` VALUES ('16', '2019-10-06', '0'); INSERT INTO `holiday` VALUES ('17', '2019-10-07', '0'); INSERT INTO `holiday` VALUES ('18', '2019-04-28', '1'); INSERT INTO `holiday` VALUES ('19', '2019-05-05', '1'); INSERT INTO `holiday` VALUES ('2', '2019-05-02', '0'); INSERT INTO `holiday` VALUES ('20', '2019-09-29', '1'); INSERT INTO `holiday` VALUES ('21', '2019-10-12', '1'); INSERT INTO `holiday` VALUES ('3', '2019-05-03', '0'); INSERT INTO `holiday` VALUES ('4', '2019-05-04', '0'); INSERT INTO `holiday` VALUES ('5', '2019-06-07', '0'); INSERT INTO `holiday` VALUES ('6', '2019-06-08', '0'); INSERT INTO `holiday` VALUES ('7', '2019-06-09', '0'); INSERT INTO `holiday` VALUES ('8', '2019-09-13', '0'); INSERT INTO `holiday` VALUES ('9', '2019-09-14', '0');