美文网首页
java 获取n个工作日后的日期(包含法定节假日、双休日、节后补

java 获取n个工作日后的日期(包含法定节假日、双休日、节后补

作者: 小波同学 | 来源:发表于2021-01-23 00:14 被阅读0次

    1、说明:

    获取n个工作日后的日期(包含法定节假日、双休日、节后补班),需要在数据库手动入库当年的法定节假日及节假日调休补班。(也可写在配置文件中读取)。

    2、工具类代码

    /**
     * @author: huangyibo
     * @Date: 2021/1/22 22:16
     * @Description: 用于查询n个工作日(包含法定节假日、双休日、调休)后的日期
     */
    public class HolidayUtils {
    
        private static final Logger logger = LoggerFactory.getLogger(HolidayUtils.class);
    
        private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    
        /**
         * 用于判断n个工作日(排除节假日、周六日包含节后补班数据)后的日期
         * @param list 节假日数据源
         * @param weekDayList 节后补班数据源
         * @param today  计算开始时间
         * @param num  多少个工作日 根据需要自行安排
         * @return
         */
        public static Date getScheduleActiveDate(List<String> list, List<String> weekDayList, Date today, int num) throws ParseException {
            String today1 = HolidayUtils.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)){
                        logger.info("HolidayUtils.getScheduleActiveDate {}: 是节假日调休补班",sdf.format(tomorrow));
                    }else{
                        logger.info("HolidayUtils.getScheduleActiveDate {}: 是正常工作日",sdf.format(tomorrow));
                    }
                    delay++;
                    today = tomorrow;
                }else if (isHoliday(sdf.format(tomorrow),list)){
    //                tomorrow = getTomorrow(tomorrow);
                    today = tomorrow;
                    logger.info("HolidayUtils.getScheduleActiveDate {}: 是节假日",sdf.format(tomorrow));
                }else if(isWeekend(sdf.format(tomorrow))){//是周六日并且不是节后补班
                    if(!isWorkWeekDay(sdf.format(tomorrow),weekDayList)){
                        today = tomorrow;
                        logger.info("HolidayUtils.getScheduleActiveDate {}: 是休息日",sdf.format(tomorrow));
                    }
    
                }
            }
            logger.info("HolidayUtils.getScheduleActiveDate, {}后{}个工作日后,日期为:{}",today1,num,sdf.format(today));
            return today;
        }
    
        /**
         * 获取明天的日期
         */
        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 dateStr) throws ParseException {
            Date date = sdf.parse(dateStr);
            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 dateStr, List<String> list) {
            if(!CollectionUtils.isEmpty(list)){
                for(int i = 0; i < list.size(); i++){
                    if(dateStr.equals(list.get(i))){
                        return true;
                    }
                }
            }
            return false;
        }
    
        /**
         * 判断是否是补班
         */
        public static boolean isWorkWeekDay(String dateStr, List<String> list) throws ParseException {
            if(!CollectionUtils.isEmpty(list)){
                for(int i = 0; i < list.size(); i++){
                    if(dateStr.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);
            return formater.format(date);
        }
    }
    

    3、测试调用

    public static void main(String[] args) {
        WorkDayQuery workDayQuery = new WorkDayQuery();
        Calendar calendar = Calendar.getInstance();
        String nowYear = String.valueOf(calendar.get(Calendar.YEAR));
        workDayQuery.setWorkYear(nowYear);
    
        //获取当前年和下一年的节假日和周末补班日期
        List<WorkDayDTO> workDayList = workDayFeign.queryWorkDayByYear(workDayQuery);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //节假日集合
        List<String> holidayList = workDayList.stream()
                .filter(holiday -> "0".equals(holiday.getType()))
                .map(holiday -> sdf.format(holiday.getWorkDay())).collect(Collectors.toList());
        //周末补班日集合
        List<String> workdayList = workDayList.stream()
                .filter(workDay -> "1".equals(workDay.getType()))
                .map(workDay -> sdf.format(workDay.getWorkDay())).collect(Collectors.toList());
                
        try {
            Date scheduleActiveDate = HolidayUtils.getScheduleActiveDate(holidayList, workdayList, new Date(), 5);
        } catch (ParseException e) {
            logger.error("工作日计算异常", e);
        }
    }
    

    4、sql脚本

    
    
    DROP TABLE IF EXISTS `holiday`;
    CREATE TABLE `holiday` (
      `GUID` varchar(36) COLLATE utf8_bin NOT NULL,
      `WORKDAY` 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');
    

    参考:
    https://www.cnblogs.com/irishua/p/11128326.html

    相关文章

      网友评论

          本文标题:java 获取n个工作日后的日期(包含法定节假日、双休日、节后补

          本文链接:https://www.haomeiwen.com/subject/bdxyzktx.html