欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

使用java获取当前日期后几天内的工作日和节假日数

程序员文章站 2022-05-15 11:34:19
...

使用java获取当前日期后几天内的工作日和节假日数

 public static String getWorkDayStart(Date startDay, int num) throws Exception {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startDay);
        int startWeekDay = calendar.get(Calendar.DAY_OF_WEEK);
        calendar.add(Calendar.DATE, num);
        int endWeekDay = calendar.get(Calendar.DAY_OF_WEEK);
        //节假日数
        int holidayNum = 0;
        //工作日数
        int workdayNum = 0;
        int number;
        if(startWeekDay > endWeekDay){
            number = endWeekDay - startWeekDay + 8;
        }else{
            number = endWeekDay - startWeekDay + 1;
        }
        if(number != 0){
            for(int i = 0;i < (num < 7?num+1:number);i++){
                if(calendar.get(Calendar.DAY_OF_WEEK) == 7 || calendar.get(Calendar.DAY_OF_WEEK) == 1){
                    holidayNum++;
                }else {
                    workdayNum++;
                }
                calendar.add(Calendar.DATE, -1);
            }
        }
        if(num >= 7){
            holidayNum = holidayNum + 2 * (num - number + 1) / 7;
            workdayNum = workdayNum + 5 * (num - number + 1) / 7;
        }
        return holidayNum + "," + workdayNum;
    }
相关标签: 后端 Java java