如有侵权,可联系删除
如有更好的工具,可以分享一下,谢谢了
需要引入的依赖 -> 依赖需要手动更新
http://6tail.cn/calendar/api.html
<dependency><groupId>cn.6tail</groupId><artifactId>lunar</artifactId><version>1.2.18</version>
</dependency>
日期工具需要的工具类
import lombok.Data;import java.util.List;/*** @desc 日期工具DTO* @author pengtianqi*/
@Data
public class HolidayDTO {/*** 节假日日期*/private List<String> holidaysList;/*** 调休日期*/private List<String> worksList;
}
工具代码
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.nlf.calendar.Holiday;
import com.nlf.calendar.util.HolidayUtil;
import lombok.extern.slf4j.Slf4j;import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;/*** @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 (startDate.isAfter(endDate)) {throw new RuntimeException("公共组件-开始时间大于结束时间");}HolidayDTO holidayDTO = holidays(startDate, endDate);//节假日List<String> holidaysList = holidayDTO.getHolidaysList();//调休日List<String> worksList = holidayDTO.getWorksList();//统计工作日的日期集合List<LocalDate> countDay = new ArrayList<>();countDay = countDay(countDay, holidaysList, worksList, startDate, endDate.plusDays(1));if (CollUtil.isEmpty(countDay)) {return 0;}log.info("日期:countDay= {}", countDay);return countDay.size();}/*** 统计工作日的日期** @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 (endDate.isEqual(timeDay)) {return countDay;} else {//获取这一天的星期String dayOfWeek = timeDay.getDayOfWeek().toString();//创建一个变量存今天是否为工作日String result = null;boolean isHolidays = SATURDAY.equals(dayOfWeek) || SUNDAY.equals(dayOfWeek);//周末是否为工作日if (isHolidays) {for (String work : worksList) {if (timeDay.toString().equals(work)) {result = WORKIND_DAY;}}}//正常周末为节假日if (StrUtil.isEmpty(result)) {if (isHolidays) {result = HOLIDAYS;}}//判断周内是否为节假日if (StrUtil.isBlank(result)) {for (String holiday : holidaysList) {if (timeDay.toString().equals(holiday)) {result = HOLIDAYS;}}}//如果前面全部没有判断上就为工作日if (StrUtil.isBlank(result)) {result = WORKIND_DAY;}if (WORKIND_DAY.equals(result)) {countDay.add(timeDay);}return queryDay(timeDay.plusDays(1), countDay, holidaysList, worksList, endDate);}}/*** 计算每年的节假日和调休日** @param startDate* @param endDate* @return*/public static HolidayDTO holidays(LocalDate startDate, LocalDate endDate) {int startDateYear = startDate.getYear();int endDateYear = endDate.getYear();//存储节假日日期List<String> holidaysList = new ArrayList<>();//存储调休日期List<String> worksList = new ArrayList<>();for (int i = startDateYear; i < endDateYear + 1; i++) {//获取节假日List<Holiday> holidays = HolidayUtil.getHolidays(startDateYear);//判断是否为为节假日for (Holiday holiday : holidays) {if (!holiday.isWork()) {holidaysList.add(holiday.getDay());} else {worksList.add(holiday.getDay());}}}HolidayDTO holidayDTO = new HolidayDTO();holidayDTO.setHolidaysList(holidaysList);holidayDTO.setWorksList(worksList);return holidayDTO;}
}