计算工作日天数

编程入门 行业动态 更新时间:2024-10-26 20:26:55

计算工作日<a href=https://www.elefans.com/category/jswz/34/1767036.html style=天数"/>

计算工作日天数

如有侵权,可联系删除 

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

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

.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;}
}

 

更多推荐

计算工作日天数

本文发布于:2023-06-27 19:08:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/915704.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:天数   工作日

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!