From f1208474f771a1c233d7425c8ed13fbaa0d521ac Mon Sep 17 00:00:00 2001 From: baoshiwei <baoshiwei@shlanbao.cn> Date: 星期三, 12 三月 2025 09:35:13 +0800 Subject: [PATCH] Merge remote-tracking branch 'origin/5.X' into 5.X --- ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/utils/DateUtils.java | 257 +++++++++++++++++++++++++++++++++++++------------- 1 files changed, 188 insertions(+), 69 deletions(-) diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/utils/DateUtils.java b/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/utils/DateUtils.java index 72178a7..41d0f6c 100644 --- a/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/utils/DateUtils.java +++ b/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/utils/DateUtils.java @@ -1,106 +1,157 @@ package org.dromara.common.core.utils; -import lombok.AccessLevel; -import lombok.NoArgsConstructor; import org.apache.commons.lang3.time.DateFormatUtils; +import org.dromara.common.core.enums.FormatsType; +import org.dromara.common.core.exception.ServiceException; import java.lang.management.ManagementFactory; 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.concurrent.TimeUnit; /** * 鏃堕棿宸ュ叿绫� * * @author ruoyi */ -@NoArgsConstructor(access = AccessLevel.PRIVATE) public class DateUtils extends org.apache.commons.lang3.time.DateUtils { - - public static final String YYYY = "yyyy"; - - public static final String YYYY_MM = "yyyy-MM"; - - public static final String YYYY_MM_DD = "yyyy-MM-dd"; - - public static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss"; - - public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss"; - private static final String[] PARSE_PATTERNS = { "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", "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"}; + @Deprecated + private DateUtils() { + } + /** - * 鑾峰彇褰撳墠Date鍨嬫棩鏈� + * 鑾峰彇褰撳墠鏃ユ湡鍜屾椂闂� * - * @return Date() 褰撳墠鏃ユ湡 + * @return 褰撳墠鏃ユ湡鍜屾椂闂寸殑 Date 瀵硅薄琛ㄧず */ public static Date getNowDate() { return new Date(); } /** - * 鑾峰彇褰撳墠鏃ユ湡, 榛樿鏍煎紡涓簓yyy-MM-dd + * 鑾峰彇褰撳墠鏃ユ湡鐨勫瓧绗︿覆琛ㄧず锛屾牸寮忎负YYYY-MM-DD * - * @return String + * @return 褰撳墠鏃ユ湡鐨勫瓧绗︿覆琛ㄧず */ public static String getDate() { - return dateTimeNow(YYYY_MM_DD); + return dateTimeNow(FormatsType.YYYY_MM_DD); } + /** + * 鑾峰彇褰撳墠鏃ユ湡鐨勫瓧绗︿覆琛ㄧず锛屾牸寮忎负yyyyMMdd + * + * @return 褰撳墠鏃ユ湡鐨勫瓧绗︿覆琛ㄧず + */ + public static String getCurrentDate() { + return DateFormatUtils.format(new Date(), FormatsType.YYYYMMDD.getTimeFormat()); + } + + /** + * 鑾峰彇褰撳墠鏃ユ湡鐨勮矾寰勬牸寮忓瓧绗︿覆锛屾牸寮忎负"yyyy/MM/dd" + * + * @return 褰撳墠鏃ユ湡鐨勮矾寰勬牸寮忓瓧绗︿覆 + */ + public static String datePath() { + Date now = new Date(); + return DateFormatUtils.format(now, FormatsType.YYYY_MM_DD_SLASH.getTimeFormat()); + } + + /** + * 鑾峰彇褰撳墠鏃堕棿鐨勫瓧绗︿覆琛ㄧず锛屾牸寮忎负YYYY-MM-DD HH:MM:SS + * + * @return 褰撳墠鏃堕棿鐨勫瓧绗︿覆琛ㄧず + */ public static String getTime() { - return dateTimeNow(YYYY_MM_DD_HH_MM_SS); + return dateTimeNow(FormatsType.YYYY_MM_DD_HH_MM_SS); } + /** + * 鑾峰彇褰撳墠鏃堕棿鐨勫瓧绗︿覆琛ㄧず锛屾牸寮忎负 "HH:MM:SS" + * + * @return 褰撳墠鏃堕棿鐨勫瓧绗︿覆琛ㄧず锛屾牸寮忎负 "HH:MM:SS" + */ + public static String getTimeWithHourMinuteSecond() { + return dateTimeNow(FormatsType.HH_MM_SS); + } + + /** + * 鑾峰彇褰撳墠鏃ユ湡鍜屾椂闂寸殑瀛楃涓茶〃绀猴紝鏍煎紡涓篩YYYMMDDHHMMSS + * + * @return 褰撳墠鏃ユ湡鍜屾椂闂寸殑瀛楃涓茶〃绀� + */ public static String dateTimeNow() { - return dateTimeNow(YYYYMMDDHHMMSS); + return dateTimeNow(FormatsType.YYYYMMDDHHMMSS); } - public static String dateTimeNow(final String format) { + /** + * 鑾峰彇褰撳墠鏃ユ湡鍜屾椂闂寸殑鎸囧畾鏍煎紡鐨勫瓧绗︿覆琛ㄧず + * + * @param format 鏃ユ湡鏃堕棿鏍煎紡锛屼緥濡�"YYYY-MM-DD HH:MM:SS" + * @return 褰撳墠鏃ユ湡鍜屾椂闂寸殑瀛楃涓茶〃绀� + */ + public static String dateTimeNow(final FormatsType format) { return parseDateToStr(format, new Date()); } - public static String dateTime(final Date date) { - return parseDateToStr(YYYY_MM_DD, date); + /** + * 灏嗘寚瀹氭棩鏈熸牸寮忓寲涓� YYYY-MM-DD 鏍煎紡鐨勫瓧绗︿覆 + * + * @param date 瑕佹牸寮忓寲鐨勬棩鏈熷璞� + * @return 鏍煎紡鍖栧悗鐨勬棩鏈熷瓧绗︿覆 + */ + public static String formatDate(final Date date) { + return parseDateToStr(FormatsType.YYYY_MM_DD, date); } - public static String parseDateToStr(final String format, final Date date) { - return new SimpleDateFormat(format).format(date); + /** + * 灏嗘寚瀹氭棩鏈熸牸寮忓寲涓� YYYY-MM-DD HH:MM:SS 鏍煎紡鐨勫瓧绗︿覆 + * + * @param date 瑕佹牸寮忓寲鐨勬棩鏈熷璞� + * @return 鏍煎紡鍖栧悗鐨勬棩鏈熸椂闂村瓧绗︿覆 + */ + public static String formatDateTime(final Date date) { + return parseDateToStr(FormatsType.YYYY_MM_DD_HH_MM_SS, date); } - public static Date dateTime(final String format, final String ts) { + /** + * 灏嗘寚瀹氭棩鏈熸寜鐓ф寚瀹氭牸寮忚繘琛屾牸寮忓寲 + * + * @param format 瑕佷娇鐢ㄧ殑鏃ユ湡鏃堕棿鏍煎紡锛屼緥濡�"YYYY-MM-DD HH:MM:SS" + * @param date 瑕佹牸寮忓寲鐨勬棩鏈熷璞� + * @return 鏍煎紡鍖栧悗鐨勬棩鏈熸椂闂村瓧绗︿覆 + */ + public static String parseDateToStr(final FormatsType format, final Date date) { + return new SimpleDateFormat(format.getTimeFormat()).format(date); + } + + /** + * 灏嗘寚瀹氭牸寮忕殑鏃ユ湡鏃堕棿瀛楃涓茶浆鎹负 Date 瀵硅薄 + * + * @param format 瑕佽В鏋愮殑鏃ユ湡鏃堕棿鏍煎紡锛屼緥濡�"YYYY-MM-DD HH:MM:SS" + * @param ts 瑕佽В鏋愮殑鏃ユ湡鏃堕棿瀛楃涓� + * @return 瑙f瀽鍚庣殑 Date 瀵硅薄 + * @throws RuntimeException 濡傛灉瑙f瀽杩囩▼涓彂鐢熷紓甯� + */ + public static Date parseDateTime(final FormatsType format, final String ts) { try { - return new SimpleDateFormat(format).parse(ts); + return new SimpleDateFormat(format.getTimeFormat()).parse(ts); } catch (ParseException e) { throw new RuntimeException(e); } } /** - * 鏃ユ湡璺緞 鍗冲勾/鏈�/鏃� 濡�2018/08/08 - */ - public static String datePath() { - Date now = new Date(); - return DateFormatUtils.format(now, "yyyy/MM/dd"); - } - - /** - * 鏃ユ湡璺緞 鍗冲勾/鏈�/鏃� 濡�20180808 - */ - public static String dateTime() { - Date now = new Date(); - return DateFormatUtils.format(now, "yyyyMMdd"); - } - - /** - * 鏃ユ湡鍨嬪瓧绗︿覆杞寲涓烘棩鏈� 鏍煎紡 + * 灏嗗璞¤浆鎹负鏃ユ湡瀵硅薄 + * + * @param str 瑕佽浆鎹㈢殑瀵硅薄锛岄�氬父鏄瓧绗︿覆 + * @return 杞崲鍚庣殑鏃ユ湡瀵硅薄锛屽鏋滆浆鎹㈠け璐ユ垨杈撳叆涓簄ull锛屽垯杩斿洖null */ public static Date parseDate(Object str) { if (str == null) { @@ -115,6 +166,8 @@ /** * 鑾峰彇鏈嶅姟鍣ㄥ惎鍔ㄦ椂闂� + * + * @return 鏈嶅姟鍣ㄥ惎鍔ㄦ椂闂寸殑 Date 瀵硅薄琛ㄧず */ public static Date getServerStartDate() { long time = ManagementFactory.getRuntimeMXBean().getStartTime(); @@ -122,35 +175,66 @@ } /** - * 璁$畻鐩稿樊澶╂暟 + * 璁$畻涓や釜鏃ユ湡涔嬮棿鐨勫ぉ鏁板樊锛堜互姣涓哄崟浣嶏級 + * + * @param date1 绗竴涓棩鏈� + * @param date2 绗簩涓棩鏈� + * @return 涓や釜鏃ユ湡涔嬮棿鐨勫ぉ鏁板樊鐨勭粷瀵瑰�� */ public static int differentDaysByMillisecond(Date date1, Date date2) { return Math.abs((int) ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24))); } /** - * 璁$畻涓や釜鏃堕棿宸� + * 璁$畻涓や釜鏃ユ湡涔嬮棿鐨勬椂闂村樊锛屽苟浠ュぉ銆佸皬鏃跺拰鍒嗛挓鐨勬牸寮忚繑鍥� + * + * @param endDate 缁撴潫鏃ユ湡 + * @param nowDate 褰撳墠鏃ユ湡 + * @return 琛ㄧず鏃堕棿宸殑瀛楃涓诧紝鏍煎紡涓�"澶� 灏忔椂 鍒嗛挓" */ public static String getDatePoor(Date endDate, Date nowDate) { - long nd = 1000 * 24 * 60 * 60; - long nh = 1000 * 60 * 60; - long nm = 1000 * 60; - // long ns = 1000; - // 鑾峰緱涓や釜鏃堕棿鐨勬绉掓椂闂村樊寮� - long diff = endDate.getTime() - nowDate.getTime(); - // 璁$畻宸灏戝ぉ - long day = diff / nd; - // 璁$畻宸灏戝皬鏃� - long hour = diff % nd / nh; - // 璁$畻宸灏戝垎閽� - long min = diff % nd % nh / nm; - // 璁$畻宸灏戠//杈撳嚭缁撴灉 - // long sec = diff % nd % nh % nm / ns; - return day + "澶�" + hour + "灏忔椂" + min + "鍒嗛挓"; + long diffInMillis = endDate.getTime() - nowDate.getTime(); + long day = TimeUnit.MILLISECONDS.toDays(diffInMillis); + long hour = TimeUnit.MILLISECONDS.toHours(diffInMillis) % 24; + long min = TimeUnit.MILLISECONDS.toMinutes(diffInMillis) % 60; + return String.format("%d澶� %d灏忔椂 %d鍒嗛挓", day, hour, min); } /** - * 澧炲姞 LocalDateTime ==> Date + * 璁$畻涓や釜鏃堕棿鐐圭殑宸�硷紙澶┿�佸皬鏃躲�佸垎閽熴�佺锛夛紝褰撳�间负0鏃朵笉鏄剧ず璇ュ崟浣� + * + * @param endDate 缁撴潫鏃堕棿 + * @param nowDate 褰撳墠鏃堕棿 + * @return 鏃堕棿宸瓧绗︿覆锛屾牸寮忎负 "x澶� x灏忔椂 x鍒嗛挓 x绉�"锛岃嫢涓� 0 鍒欎笉鏄剧ず + */ + public static String getTimeDifference(Date endDate, Date nowDate) { + long diffInMillis = endDate.getTime() - nowDate.getTime(); + long day = TimeUnit.MILLISECONDS.toDays(diffInMillis); + long hour = TimeUnit.MILLISECONDS.toHours(diffInMillis) % 24; + long min = TimeUnit.MILLISECONDS.toMinutes(diffInMillis) % 60; + long sec = TimeUnit.MILLISECONDS.toSeconds(diffInMillis) % 60; + // 鏋勫缓鏃堕棿宸瓧绗︿覆锛屾潯浠舵槸鍊间笉涓�0鎵嶆樉绀� + StringBuilder result = new StringBuilder(); + if (day > 0) { + result.append(String.format("%d澶� ", day)); + } + if (hour > 0) { + result.append(String.format("%d灏忔椂 ", hour)); + } + if (min > 0) { + result.append(String.format("%d鍒嗛挓 ", min)); + } + if (sec > 0) { + result.append(String.format("%d绉�", sec)); + } + return result.length() > 0 ? result.toString().trim() : "0绉�"; + } + + /** + * 灏� LocalDateTime 瀵硅薄杞崲涓� Date 瀵硅薄 + * + * @param temporalAccessor 瑕佽浆鎹㈢殑 LocalDateTime 瀵硅薄 + * @return 杞崲鍚庣殑 Date 瀵硅薄 */ public static Date toDate(LocalDateTime temporalAccessor) { ZonedDateTime zdt = temporalAccessor.atZone(ZoneId.systemDefault()); @@ -158,11 +242,46 @@ } /** - * 澧炲姞 LocalDate ==> Date + * 灏� LocalDate 瀵硅薄杞崲涓� Date 瀵硅薄 + * + * @param temporalAccessor 瑕佽浆鎹㈢殑 LocalDate 瀵硅薄 + * @return 杞崲鍚庣殑 Date 瀵硅薄 */ public static Date toDate(LocalDate temporalAccessor) { LocalDateTime localDateTime = LocalDateTime.of(temporalAccessor, LocalTime.of(0, 0, 0)); ZonedDateTime zdt = localDateTime.atZone(ZoneId.systemDefault()); return Date.from(zdt.toInstant()); } + + /** + * 鏍¢獙鏃ユ湡鑼冨洿 + * + * @param startDate 寮�濮嬫棩鏈� + * @param endDate 缁撴潫鏃ユ湡 + * @param maxValue 鏈�澶ф椂闂磋法搴︾殑闄愬埗鍊� + * @param unit 鏃堕棿璺ㄥ害鐨勫崟浣嶏紝鍙�夋嫨 "DAYS"銆�"HOURS" 鎴� "MINUTES" + */ + public static void validateDateRange(Date startDate, Date endDate, int maxValue, TimeUnit unit) { + // 鏍¢獙缁撴潫鏃ユ湡涓嶈兘鏃╀簬寮�濮嬫棩鏈� + if (endDate.before(startDate)) { + throw new ServiceException("缁撴潫鏃ユ湡涓嶈兘鏃╀簬寮�濮嬫棩鏈�"); + } + + // 璁$畻鏃堕棿璺ㄥ害 + long diffInMillis = endDate.getTime() - startDate.getTime(); + + // 鏍规嵁鍗曚綅杞崲鏃堕棿璺ㄥ害 + long diff = switch (unit) { + case DAYS -> TimeUnit.MILLISECONDS.toDays(diffInMillis); + case HOURS -> TimeUnit.MILLISECONDS.toHours(diffInMillis); + case MINUTES -> TimeUnit.MILLISECONDS.toMinutes(diffInMillis); + default -> throw new IllegalArgumentException("涓嶆敮鎸佺殑鏃堕棿鍗曚綅"); + }; + + // 鏍¢獙鏃堕棿璺ㄥ害涓嶈秴杩囨渶澶ч檺鍒� + if (diff > maxValue) { + throw new ServiceException("鏈�澶ф椂闂磋法搴︿负 " + maxValue + " " + unit.toString().toLowerCase()); + } + } + } -- Gitblit v1.9.3