xiangpei
2025-03-31 1698792d4299a0b81b9695d8a56e3d3088c7a7ee
common/src/main/java/com/ycl/common/utils/DateUtils.java
@@ -4,13 +4,11 @@
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.*;
import java.util.Date;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.lang.Nullable;
@@ -37,6 +35,94 @@
            "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
            "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};
    private static String isHoliday = "1-1,1-28,1-29,1-30,1-31,2-1,2-2,2-3,2-4,"
            +"4-4,4-5,4-6,5-1,5-2,5-3,5-4,5-5,5-31,6-1,6-2,10-1,10-2,10-3,"
            +"10-4,10-5,10-6,10-7,10-8";
    /**
     * 获取两个时间内,除去周末周日以及法定节假日的 小时总数。
     * @param startDate
     * @param endDate
     * @return
     */
    public static long getWorkingHours(Date startDate,Date endDate){
        long workingHours = 0;
        //转换日期格式
        LocalDateTime startTime = startDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        LocalDateTime endTime = endDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        if(startTime.isAfter(endTime)){
            throw new RuntimeException("开始时间不能晚于结束时间");
        }
        LocalDateTime varTime = startTime;
        //节假日日期(2025为列)
        Set<LocalDate> holidays = new HashSet<>();
        String[] holiday = isHoliday.split(",");
        for (String str : holiday) {
            String[] md = str.split("-");
            int month = Integer.parseInt(md[0]);
            int day = Integer.parseInt(md[1]);
            holidays.add(LocalDate.of(2025,month,day));
        }
        while (varTime.isBefore(endTime)){
            boolean isWorkDay = varTime.getDayOfWeek().getValue() < 6
                    && !holidays.contains(varTime.toLocalDate());
            // 判断是否是工作日(非周末 + 非节假日)
            if (isWorkDay){
                workingHours++;
            }
            varTime = varTime.plusHours(1);
        }
        return workingHours;
    }
    /**
     * 获取两个时间内,除去周末周日以及法定节假日的 秒总数。
     * @param startDate
     * @param endDate
     * @return
     */
    public static long getWorkingSed(Date startDate,Date endDate){
        long workingHours = 0;
        long workingMill = 0;
        //转换日期格式
        LocalDateTime startTime = startDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        LocalDateTime endTime = endDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        if(startTime.isAfter(endTime)){
            throw new RuntimeException("开始时间不能晚于结束时间");
        }
        LocalDateTime varTime = startTime;
        //节假日日期(2025为列)
        Set<LocalDate> holidays = new HashSet<>();
        String[] holiday = isHoliday.split(",");
        for (String str : holiday) {
            String[] md = str.split("-");
            int month = Integer.parseInt(md[0]);
            int day = Integer.parseInt(md[1]);
            holidays.add(LocalDate.of(2025,month,day));
        }
        while (varTime.isBefore(endTime)){
            boolean isWorkDay = varTime.getDayOfWeek().getValue() < 6
                    && !holidays.contains(varTime.toLocalDate());
            long diffMill = endTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli() - varTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
            // 判断是否是工作日(非周末 + 非节假日)
            if (isWorkDay){
                if (diffMill < 3600000) {
                    // 如果两个时间不到一小时差距
                    workingMill += diffMill;
                    break;
                }
                workingHours++;
                workingMill += 3600000;
            }
            varTime = varTime.plusHours(1);
        }
        return workingMill / 1000;
    }
    /**
     * 获取当前Date型日期
     *