From 4fabfe4dbd2eb28d07a4350597d314958cc1c281 Mon Sep 17 00:00:00 2001 From: MrShi <1878285526@qq.com> Date: 星期四, 09 十月 2025 11:16:43 +0800 Subject: [PATCH] 优化 --- server/services/src/main/java/com/doumee/core/utils/DateUtil.java | 127 +++++++++++++++++++++++++++++++++++++++-- 1 files changed, 119 insertions(+), 8 deletions(-) diff --git a/server/services/src/main/java/com/doumee/core/utils/DateUtil.java b/server/services/src/main/java/com/doumee/core/utils/DateUtil.java index 658895b..f585aa6 100644 --- a/server/services/src/main/java/com/doumee/core/utils/DateUtil.java +++ b/server/services/src/main/java/com/doumee/core/utils/DateUtil.java @@ -10,6 +10,9 @@ import java.text.DateFormatSymbols; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.time.ZonedDateTime; import java.util.Date; import java.util.*; @@ -39,7 +42,18 @@ public DateUtil() { } + public static Date getDateFromLocalDateTime( LocalDateTime localDateTime) { + try { + ZoneId zoneId = ZoneId.systemDefault(); // 鑾峰彇绯荤粺榛樿鏃跺尯 + ZonedDateTime zonedDateTime = localDateTime.atZone(zoneId); // 杞崲涓哄甫鏃跺尯鐨勬棩鏈熸椂闂� + Date date = Date.from(zonedDateTime.toInstant()); // 杞崲涓篋ate + return date; + }catch (Exception e){ + } + return null; + + } public static Date StringToDate2(String DATE) { if(StringUtils.isBlank(DATE)){ return null; @@ -52,6 +66,56 @@ exception.printStackTrace(); } return dt1; + } + + + public static List<Date> getDateList(Date dBegin, Date dEnd) { + int i = 1; + //鏃ユ湡宸ュ叿绫诲噯澶� + DateFormat format = new SimpleDateFormat("yyyy-MM-dd"); + //璁剧疆寮�濮嬫椂闂� + Calendar calBegin = Calendar.getInstance(); + calBegin.setTime(dBegin); + int weekNumber = calBegin.get(Calendar.DAY_OF_WEEK) - 1; + //璁剧疆缁撴潫鏃堕棿 + Calendar calEnd = Calendar.getInstance(); + calEnd.setTime(dEnd); + //瑁呰繑鍥炵殑鏃ユ湡闆嗗悎瀹瑰櫒 + List<Date> dateList = new ArrayList<Date>(); + dateList.add(dBegin); + //灏嗙涓�涓湀娣诲姞閲岄潰鍘� + while (dEnd.after(calBegin.getTime())) { + calBegin.add(Calendar.DAY_OF_MONTH, 1); + Date date = calBegin.getTime(); + dateList.add(date); + } + return dateList; + } + + + + public static Date StringToDateFormat(String DATE,String format) { + if(StringUtils.isBlank(DATE)){ + return null; + } + DateFormat df = new SimpleDateFormat(format); + Date dt1 = null; + try { + dt1 = df.parse(DATE); + } catch (Exception exception) { + exception.printStackTrace(); + } + return dt1; + } + + + public static String getXDaysAfter(Date date, Integer days){ + Timestamp currentTimestamp = new Timestamp(date.getTime()); + Calendar calendar = Calendar.getInstance(); + calendar.setTime(currentTimestamp); + calendar.add(Calendar.DAY_OF_YEAR, days); // 鍦ㄥ綋鍓嶆椂闂村熀纭�涓婃坊鍔犳寚瀹氱殑澶╂暟 + SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); + return formatter.format(calendar.getTime()); } /** @@ -291,6 +355,15 @@ return nowDate; } catch (Exception e) { throw e; + } + } + public static String getShortDateStr(Date date) { + String nowDate = ""; + try { + nowDate = sdfShort.format(date); + return nowDate; + } catch (Exception e) { + return null; } } @@ -2131,19 +2204,19 @@ /** * 寰楀嚭涓や釜鏃ユ湡涔嬮棿鐨勯棿闅斿ぉ鏁� * - * @param strFromDate + * @param smallDate * 鏍煎紡涓簓yyy-MM-dd - * @param strToDate + * @param bigDate * 鏍煎紡涓簓yyy-MM-dd * @return int */ - public static long getBetweenDays(String strFromDate, String strToDate) { + public static long getBetweenDays(String smallDate, String bigDate) { long m_intervalday = 0;// 鍒濆鍖栨椂闂撮棿闅旂殑鍊间负0 // 浣跨敤鐨勬椂闂存牸寮忎负yyyy-MM-dd SimpleDateFormat m_simpledateformat = new SimpleDateFormat("yyyy-MM-dd"); try { - Date fromDate = m_simpledateformat.parse(strFromDate); - Date toDate = m_simpledateformat.parse(strToDate); + Date fromDate = m_simpledateformat.parse(smallDate); + Date toDate = m_simpledateformat.parse(bigDate); m_intervalday = toDate.getTime() - fromDate.getTime();// 璁$畻鎵�寰椾负寰鏁� m_intervalday = m_intervalday / 1000 / 60 / 60 / 24;// 璁$畻鎵�寰楃殑澶╂暟 @@ -2300,7 +2373,7 @@ } public static Date getDateFromString2(String strDate) { if (StringUtils.isEmpty(strDate)) { - return new Date(System.currentTimeMillis()); + return null; } try { return sdfLongTimePlus.parse(strDate); @@ -2739,7 +2812,7 @@ public static void main(String[] args) { try { - System.out.println(RandomStringUtils.randomNumeric(3)); + System.out.println(DateUtil.getCurrDate()); } catch (Exception ex) { ex.printStackTrace(); } @@ -2994,6 +3067,44 @@ return cal.getTime(); } + + /** + * 2涓棩鏈熶箣闂寸浉宸垎閽熸暟 + * @param begin + * @param end + * @return + */ + public static Integer betweenMin(Date begin, Date end) { + if(begin ==null ){ + return 0; + } + long createTime =begin.getTime();//鑾峰彇鍒涘缓鏃堕棿鐨勬椂闂存埑 + long currentTime =Objects.isNull(end)?System.currentTimeMillis():end.getTime();//鑾峰彇褰撳墠鏃堕棿鐨勬椂闂存埑 + long diff=(currentTime-createTime)/1000/60;//鑾峰彇涓や釜鏃堕棿鐩稿樊鐨勫垎閽� + return (int)diff + 1 ; + } + + public static Integer between(Date begin, Date end) { + if(begin ==null ){ + return 0; + } + long createTime =begin.getTime();//鑾峰彇鍒涘缓鏃堕棿鐨勬椂闂存埑 + long currentTime =Objects.isNull(end)?System.currentTimeMillis():end.getTime();//鑾峰彇褰撳墠鏃堕棿鐨勬椂闂存埑 + long diff=(currentTime-createTime)/1000/60;//鑾峰彇涓や釜鏃堕棿鐩稿樊鐨勫垎閽� + return (int)diff; + } + + public static Integer betweenSeconds(Date begin, Date end) { + if(begin ==null ){ + return 0; + } + long createTime =begin.getTime();//鑾峰彇鍒涘缓鏃堕棿鐨勬椂闂存埑 + long currentTime =Objects.isNull(end)?System.currentTimeMillis():end.getTime();//鑾峰彇褰撳墠鏃堕棿鐨勬椂闂存埑 + long diff=(currentTime-createTime)/1000;//鑾峰彇涓や釜鏃堕棿鐩稿樊鐨勫垎閽� + return (int)diff; + } + + /** * 璁$畻鑰楁椂 * @param start @@ -3013,4 +3124,4 @@ return consuming.toString(); } -} \ No newline at end of file +} -- Gitblit v1.9.3