From 1698792d4299a0b81b9695d8a56e3d3088c7a7ee Mon Sep 17 00:00:00 2001 From: xiangpei <xiangpei@timesnew.cn> Date: 星期一, 31 三月 2025 11:39:50 +0800 Subject: [PATCH] 超时计算排除节假日,超时统计调整 --- common/src/main/java/com/ycl/common/utils/DateUtils.java | 52 +++++++++++++++++++++++++++++++++++++++++++++++----- 1 files changed, 47 insertions(+), 5 deletions(-) diff --git a/common/src/main/java/com/ycl/common/utils/DateUtils.java b/common/src/main/java/com/ycl/common/utils/DateUtils.java index 3780ccc..e6de789 100644 --- a/common/src/main/java/com/ycl/common/utils/DateUtils.java +++ b/common/src/main/java/com/ycl/common/utils/DateUtils.java @@ -4,11 +4,7 @@ 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; @@ -80,6 +76,52 @@ 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鍨嬫棩鏈� -- Gitblit v1.8.0