美文网首页
输出工作日

输出工作日

作者: 暖熊熊 | 来源:发表于2019-02-26 17:51 被阅读0次

    根据给定起止日期输出范围内需要工作的日期(去除法定节假日、周末等)

    package com.ghw.springboot.oa;
    
    import org.apache.commons.lang3.StringUtils;
    import org.apache.commons.lang3.time.DateFormatUtils;
    import org.apache.commons.lang3.time.DateUtils;
    
    import java.text.ParseException;
    import java.util.ArrayList;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.List;
    
    /**
     * 输出工作日(根据给定起止日期输出范围内需要工作的日期(去除法定节假日、周末等))
     */
    public class JieJiaRi {
    
        private static Boolean isAdd(Date date) {
            Calendar c = DateUtils.toCalendar(date);
            int i = c.get(Calendar.DAY_OF_WEEK);
            //如果是周末,判断是否要加班
            if (i == 1 || i == 7) {
                return needWork(date, "02");
                //如果是周内判断是否调休
            } else {
                return needWork(date, "01");
            }
        }
    
        /**
         * 工作日是否需要工作
         *
         * @param date 日期
         * @param type 01周内,02周末
         * @return true 需要工作,false 不需要工作
         */
        private static boolean needWork(Date date, String type) {
            String offDay2019 = "2019-01-01,2019-02-04,2019-02-05,2019-02-06,2019-02-07,2019-02-08,2019-04-05,2019-05-01,2019-06-07,2019-09-13,2019-10-01,2019-10-02,2019-10-03,2019-10-04,2019-10-07";
            String workDay2019 = "2019-02-02,2019-02-03,2019-09-29,2019-10-12";
            String format = DateFormatUtils.format(date, "yyyy-MM-dd");
            if (StringUtils.contains(offDay2019, format)) {
                return false;
            }
            if (StringUtils.contains(workDay2019, format)) {
                return true;
            }
            if ("02".equals(type)) {
                return false;
            }
            return true;
        }
    
        public static void main(String[] args) throws ParseException {
            String beginDay = "2019-02-01";
            String endDay = "2019-02-28";
            Date begin = DateUtils.parseDate(beginDay, "yyyy-MM-dd");
            Date end = DateUtils.parseDate(endDay, "yyyy-MM-dd");
            List<Date> list = new ArrayList<>();
            while (!DateUtils.isSameDay(begin, DateUtils.addDays(end, 1))) {
                if (isAdd(begin)) {
                    list.add(begin);
                }
                begin = DateUtils.addDays(begin, 1);
            }
    
            for (int i = 0; i < list.size(); i++) {
                Date date = list.get(i);
                System.out.println(DateFormatUtils.format(date, "yyyy-MM-dd"));
            }
            System.out.println(list.size());
        }
    
    }
    
    

    相关文章

      网友评论

          本文标题:输出工作日

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