计算工作日天数

时间:2025-03-10 08:41:37

如有侵权,可联系删除 

如有更好的工具,可以分享一下,谢谢了

需要引入的依赖 -> 依赖需要手动更新

/calendar/

<dependency>
	<groupId>cn.6tail</groupId>
	<artifactId>lunar</artifactId>
	<version>1.2.18</version>
</dependency>

日期工具需要的工具类

import ;

import ;

/**
 * @desc 日期工具DTO
 * @author pengtianqi
 */
@Data
public class HolidayDTO {

    /**
     * 节假日日期
     */
    private List<String> holidaysList;

    /**
     * 调休日期
     */
    private List<String> worksList;
}

工具代码 

import ;
import ;
import ;
import ;
import .slf4j.Slf4j;

import ;
import ;
import ;

/**
 * @author pengtianqi
 * @Desc 计算两个时间段的工作日数量
 */
@Slf4j
public class DateUtil {

    /**
     * 周六/周天/工作日/节假日定义变量
     */
    public static final String SATURDAY = "SATURDAY";
    public static final String SUNDAY = "SUNDAY";
    public static final String WORKIND_DAY = "工作日";
    public static final String HOLIDAYS = "节假日";

    /**
     * 统计两个时间段的工作日数量
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public static Integer countNumber(LocalDate startDate, LocalDate endDate) {
        if ((endDate)) {
            throw new RuntimeException("公共组件-开始时间大于结束时间");
        }
        HolidayDTO holidayDTO = holidays(startDate, endDate);
        //节假日
        List<String> holidaysList = ();
        //调休日
        List<String> worksList = ();
        //统计工作日的日期集合
        List<LocalDate> countDay = new ArrayList<>();
        countDay = countDay(countDay, holidaysList, worksList, startDate, (1));
        if ((countDay)) {
            return 0;
        }
        ("日期:countDay= {}", countDay);
        return ();
    }

    /**
     * 统计工作日的日期
     *
     * @param countDay
     * @param holidaysList
     * @param worksList
     * @param startDate
     * @param endDate
     * @return
     */
    public static List<LocalDate> countDay(List<LocalDate> countDay, List<String> holidaysList, List<String> worksList, LocalDate startDate, LocalDate endDate) {
        return queryDay(startDate, countDay, holidaysList, worksList, endDate);
    }

    /**
     * 查询是否为工作日
     *
     * @param timeDay
     * @param countDay
     * @param holidaysList
     * @param worksList
     * @param endDate
     * @return
     */
    public static List<LocalDate> queryDay(LocalDate timeDay, List<LocalDate> countDay, List<String> holidaysList, List<String> worksList, LocalDate endDate) {
        if ((timeDay)) {
            return countDay;
        } else {
            //获取这一天的星期
            String dayOfWeek = ().toString();
            //创建一个变量存今天是否为工作日
            String result = null;
            boolean isHolidays = (dayOfWeek) || (dayOfWeek);
            //周末是否为工作日
            if (isHolidays) {
                for (String work : worksList) {
                    if (().equals(work)) {
                        result = WORKIND_DAY;
                    }
                }
            }
            //正常周末为节假日
            if ((result)) {
                if (isHolidays) {
                    result = HOLIDAYS;
                }
            }
            //判断周内是否为节假日
            if ((result)) {
                for (String holiday : holidaysList) {
                    if (().equals(holiday)) {
                        result = HOLIDAYS;
                    }
                }
            }
            //如果前面全部没有判断上就为工作日
            if ((result)) {
                result = WORKIND_DAY;
            }
            if (WORKIND_DAY.equals(result)) {
                (timeDay);
            }
            return queryDay((1), countDay, holidaysList, worksList, endDate);
        }
    }

    /**
     * 计算每年的节假日和调休日
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public static HolidayDTO holidays(LocalDate startDate, LocalDate endDate) {
        int startDateYear = ();
        int endDateYear = ();
        //存储节假日日期
        List<String> holidaysList = new ArrayList<>();
        //存储调休日期
        List<String> worksList = new ArrayList<>();
        for (int i = startDateYear; i < endDateYear + 1; i++) {
            //获取节假日
            List<Holiday> holidays = (startDateYear);
            //判断是否为为节假日
            for (Holiday holiday : holidays) {
                if (!()) {
                    (());
                } else {
                    (());
                }
            }
        }
        HolidayDTO holidayDTO = new HolidayDTO();
        (holidaysList);
        (worksList);
        return holidayDTO;
    }
}