From 50fb58286ed3b718c39a97e0987ee7561a295651 Mon Sep 17 00:00:00 2001 From: k94314517 <8417338+k94314517@user.noreply.gitee.com> Date: 星期五, 04 七月 2025 17:56:41 +0800 Subject: [PATCH] git ch --- server/service/src/main/java/com/doumee/core/utils/DateUtil.java | 336 +++++++++++++++++++++++++++++-------------------------- 1 files changed, 177 insertions(+), 159 deletions(-) diff --git a/server/service/src/main/java/com/doumee/core/utils/DateUtil.java b/server/service/src/main/java/com/doumee/core/utils/DateUtil.java index 1acf22d..065ac27 100644 --- a/server/service/src/main/java/com/doumee/core/utils/DateUtil.java +++ b/server/service/src/main/java/com/doumee/core/utils/DateUtil.java @@ -1,6 +1,8 @@ package com.doumee.core.utils; +import java.math.BigDecimal; +import java.math.RoundingMode; import java.sql.Timestamp; import java.text.DateFormat; import java.text.DateFormatSymbols; @@ -8,6 +10,7 @@ import java.text.SimpleDateFormat; import java.time.LocalDate; import java.time.Period; +import java.time.ZoneId; import java.util.Calendar; import java.util.Date; import java.util.Enumeration; @@ -91,10 +94,12 @@ /** * 寰楀嚭涓や釜鏃ユ湡涔嬮棿鐨勯棿闅斿皬鏃� * - * @param fromDate + * @param fromDate 闈犲墠鐨勬椂闂� * 鏍煎紡涓簓yyy-MM-dd - * @param toDate + * @param toDate 闈犲悗鐨勬椂闂� * 鏍煎紡涓簓yyy-MM-dd + * DateUtil.getBetweenHours(DateUtil.fromStringToDate("yyyy-MM-dd HH:mm:ss","2025-05-26 10:46:43"), + * DateUtil.fromStringToDate("yyyy-MM-dd HH:mm:ss","2025-05-26 07:41:43")) 杩斿洖 -2灏忔椂 * @return int */ public static long getBetweenHours(Date fromDate, Date toDate) { @@ -103,6 +108,33 @@ try { m_intervalday = toDate.getTime() - fromDate.getTime();// 璁$畻鎵�寰椾负寰鏁� m_intervalday = m_intervalday / 1000 / 60 / 60;// 璁$畻鎵�寰楃殑澶╂暟 + + return m_intervalday + 1; + } catch (Exception e) { + return Integer.MIN_VALUE; + } + } + + public static long getBetweenMin(Date fromDate, Date toDate) { + long m_intervalday = 0;// 鍒濆鍖栨椂闂撮棿闅旂殑鍊间负0 + // 浣跨敤鐨勬椂闂存牸寮忎负yyyy-MM-dd + try { + m_intervalday = toDate.getTime() - fromDate.getTime();// 璁$畻鎵�寰椾负寰鏁� + m_intervalday = m_intervalday / 1000 / 60 ;// 璁$畻鎵�寰楃殑澶╂暟 + + return m_intervalday + 1; + } catch (Exception e) { + return Integer.MIN_VALUE; + } + } + + + public static long getBetweenDay(Date fromDate, Date toDate) { + long m_intervalday = 0;// 鍒濆鍖栨椂闂撮棿闅旂殑鍊间负0 + // 浣跨敤鐨勬椂闂存牸寮忎负yyyy-MM-dd + try { + m_intervalday = toDate.getTime() - fromDate.getTime();// 璁$畻鎵�寰椾负寰鏁� + m_intervalday = m_intervalday / 1000 / 60 / 60 / 24 ;// 璁$畻鎵�寰楃殑澶╂暟 return m_intervalday + 1; } catch (Exception e) { @@ -350,6 +382,8 @@ throw e; } } + + /** * Descrption:鍙栧緱褰撳墠鏃ユ湡,鏍煎紡涓�:yyyy-MM-dd HH:mm:ss @@ -884,95 +918,95 @@ * @return 杞崲寰楀埌鐨勬棩鏈� */ @SuppressWarnings("unchecked") - public static Date stringToDate(String strDate, String oracleFormat) { - if (strDate == null) - return null; - Hashtable<Integer, String> h = new Hashtable<Integer, String>(); - String javaFormat = new String(); - String s = oracleFormat.toLowerCase(); - if (s.indexOf("yyyy") != -1) - h.put(new Integer(s.indexOf("yyyy")), "yyyy"); - else if (s.indexOf("yy") != -1) - h.put(new Integer(s.indexOf("yy")), "yy"); - if (s.indexOf("mm") != -1) - h.put(new Integer(s.indexOf("mm")), "MM"); - - if (s.indexOf("dd") != -1) - h.put(new Integer(s.indexOf("dd")), "dd"); - if (s.indexOf("hh24") != -1) - h.put(new Integer(s.indexOf("hh24")), "HH"); - if (s.indexOf("mi") != -1) - h.put(new Integer(s.indexOf("mi")), "mm"); - if (s.indexOf("ss") != -1) - h.put(new Integer(s.indexOf("ss")), "ss"); - - int intStart = 0; - while (s.indexOf("-", intStart) != -1) { - intStart = s.indexOf("-", intStart); - h.put(new Integer(intStart), "-"); - intStart++; - } - - intStart = 0; - while (s.indexOf("/", intStart) != -1) { - intStart = s.indexOf("/", intStart); - h.put(new Integer(intStart), "/"); - intStart++; - } - - intStart = 0; - while (s.indexOf(" ", intStart) != -1) { - intStart = s.indexOf(" ", intStart); - h.put(new Integer(intStart), " "); - intStart++; - } - - intStart = 0; - while (s.indexOf(":", intStart) != -1) { - intStart = s.indexOf(":", intStart); - h.put(new Integer(intStart), ":"); - intStart++; - } - - if (s.indexOf("骞�") != -1) - h.put(new Integer(s.indexOf("骞�")), "骞�"); - if (s.indexOf("鏈�") != -1) - h.put(new Integer(s.indexOf("鏈�")), "鏈�"); - if (s.indexOf("鏃�") != -1) - h.put(new Integer(s.indexOf("鏃�")), "鏃�"); - if (s.indexOf("鏃�") != -1) - h.put(new Integer(s.indexOf("鏃�")), "鏃�"); - if (s.indexOf("鍒�") != -1) - h.put(new Integer(s.indexOf("鍒�")), "鍒�"); - if (s.indexOf("绉�") != -1) - h.put(new Integer(s.indexOf("绉�")), "绉�"); - - int i = 0; - while (h.size() != 0) { - Enumeration e = h.keys(); - int n = 0; - while (e.hasMoreElements()) { - i = ((Integer) e.nextElement()).intValue(); - if (i >= n) - n = i; - } - String temp = (String) h.get(new Integer(n)); - h.remove(new Integer(n)); - - javaFormat = temp + javaFormat; - } - SimpleDateFormat df = new SimpleDateFormat(javaFormat); - - Date myDate = new Date(); - try { - myDate = df.parse(strDate); - } catch (Exception e) { - // e.printStackTrace(); - return null; - } - - return myDate; - } +// public static Date stringToDate(String strDate, String oracleFormat) { +// if (strDate == null) +// return null; +// Hashtable<Integer, String> h = new Hashtable<Integer, String>(); +// String javaFormat = new String(); +// String s = oracleFormat.toLowerCase(); +// if (s.indexOf("yyyy") != -1) +// h.put(new Integer(s.indexOf("yyyy")), "yyyy"); +// else if (s.indexOf("yy") != -1) +// h.put(new Integer(s.indexOf("yy")), "yy"); +// if (s.indexOf("mm") != -1) +// h.put(new Integer(s.indexOf("mm")), "MM"); +// +// if (s.indexOf("dd") != -1) +// h.put(new Integer(s.indexOf("dd")), "dd"); +// if (s.indexOf("hh24") != -1) +// h.put(new Integer(s.indexOf("hh24")), "HH"); +// if (s.indexOf("mi") != -1) +// h.put(new Integer(s.indexOf("mi")), "mm"); +// if (s.indexOf("ss") != -1) +// h.put(new Integer(s.indexOf("ss")), "ss"); +// +// int intStart = 0; +// while (s.indexOf("-", intStart) != -1) { +// intStart = s.indexOf("-", intStart); +// h.put(new Integer(intStart), "-"); +// intStart++; +// } +// +// intStart = 0; +// while (s.indexOf("/", intStart) != -1) { +// intStart = s.indexOf("/", intStart); +// h.put(new Integer(intStart), "/"); +// intStart++; +// } +// +// intStart = 0; +// while (s.indexOf(" ", intStart) != -1) { +// intStart = s.indexOf(" ", intStart); +// h.put(new Integer(intStart), " "); +// intStart++; +// } +// +// intStart = 0; +// while (s.indexOf(":", intStart) != -1) { +// intStart = s.indexOf(":", intStart); +// h.put(new Integer(intStart), ":"); +// intStart++; +// } +// +// if (s.indexOf("骞�") != -1) +// h.put(new Integer(s.indexOf("骞�")), "骞�"); +// if (s.indexOf("鏈�") != -1) +// h.put(new Integer(s.indexOf("鏈�")), "鏈�"); +// if (s.indexOf("鏃�") != -1) +// h.put(new Integer(s.indexOf("鏃�")), "鏃�"); +// if (s.indexOf("鏃�") != -1) +// h.put(new Integer(s.indexOf("鏃�")), "鏃�"); +// if (s.indexOf("鍒�") != -1) +// h.put(new Integer(s.indexOf("鍒�")), "鍒�"); +// if (s.indexOf("绉�") != -1) +// h.put(new Integer(s.indexOf("绉�")), "绉�"); +// +// int i = 0; +// while (h.size() != 0) { +// Enumeration e = h.keys(); +// int n = 0; +// while (e.hasMoreElements()) { +// i = ((Integer) e.nextElement()).intValue(); +// if (i >= n) +// n = i; +// } +// String temp = (String) h.get(new Integer(n)); +// h.remove(new Integer(n)); +// +// javaFormat = temp + javaFormat; +// } +// SimpleDateFormat df = new SimpleDateFormat(javaFormat); +// +// Date myDate = new Date(); +// try { +// myDate = df.parse(strDate); +// } catch (Exception e) { +// // e.printStackTrace(); +// return null; +// } +// +// return myDate; +// } public static Date StringToDate(String DATE1) { DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); @@ -3031,6 +3065,10 @@ return DateToString(date, "yyyyMMddHHmmss"); } + public static String getLongDateTime(Date date) { + return DateToString(date, "yyyy-MM-dd HH:mm:ss"); + } + public static String DateToString(Date date, String dateStyle) { String dateString = null; if (dateStyle != null) { @@ -3075,7 +3113,25 @@ return cal.getTime(); } + /** + * 鑾峰彇鏌愪釜鏃堕棿X鍒嗛挓鍚庣殑鏃堕棿 + * @param minutes + * @param date + * @return + */ + public static Date afterMinutesByDate(Integer minutes,Date date){ + Calendar cal = Calendar.getInstance(); + cal.setTime(date); + cal.add(Calendar.MINUTE,minutes); + return cal.getTime(); + } + public static Date afterDayByDate(Integer days,Date date){ + Calendar cal = Calendar.getInstance(); + cal.setTime(date); + cal.add(Calendar.DATE,days); + return cal.getTime(); + } /** * 璁$畻澶氬皯鑷劧骞�/鏈�/鏃ュ悗鐨勬棩鏈� * @param startDate 寮�濮嬫棩鏈� @@ -3147,76 +3203,38 @@ } - public static void main(String[] args) { -// System.out.println(DateUtil.DateToStr(DateUtil.afterDateByType( -// DateUtil.stringToDate("2024-02-01","yyyy-MM-dd") -// ,1,1),"yyyy-MM-dd HH:mm:ss")); + public static void main(String[] args) throws Exception{ + System.out.println(DateUtil.DateToStr( + DateUtil.afterDateByType(new Date(),1,-11),"yyyy-MM") + ); +// +// System.out.println(DateUtil.getBetweenHours(DateUtil.fromStringToDate("yyyy-MM-dd HH:mm:ss","2025-05-26 10:46:43"), +// DateUtil.fromStringToDate("yyyy-MM-dd HH:mm:ss","2025-05-26 07:41:43"))); +// ; -// System.out.println( DateUtil.daysBetweenDates(DateUtil.stringToDate("2024-02-29","yyyy-MM-dd"), -// DateUtil.stringToDate("2024-02-01","yyyy-MM-dd")) -// ); -// -// System.out.println(DateUtil.calculateBetween(DateUtil.StringToDate("2024-02-29 23:59:59"),DateUtil.StringToDate("2024-02-01 00:00:00"),0)); - - -// System.out.println( -// DateUtil.DateToStr( -// -// DateUtil.getMontageDate(DateUtil.StringToDate("2024-02-29 21:59:59"),1) ,"yyyy-MM-dd HH:mm:ss" -// ) -// ); -// -// -// -// System.out.println( -// -// DateUtil.DateToStr( -// -// DateUtil.getMontageDate(DateUtil.StringToDate("2024-02-29 21:59:59"),2) ,"yyyy-MM-dd HH:mm:ss" -// ) -// -// ); -// -// System.out.println( -// -// DateUtil.DateToStr( -// -// DateUtil.getMontageDate(DateUtil.StringToDate("2024-02-29 21:59:59"),3) ,"yyyy-MM-dd HH:mm:ss" -// ) - -// ); - - LocalDate startDate= LocalDate.of(2021,2,28); - LocalDate endDate =LocalDate.of(2022,4,5); - Period period = Period.between(startDate,endDate); - int months = period.getYears()* 12 + period.getMonths(); - System.out.println("Months between the two dates: " + months); - - -/* Calendar calo = Calendar.getInstance(); - Calendar caln = Calendar.getInstance(); // ,"yyyy-MM-dd HH:mm:ss" - caln.setTime(DateUtil.StringToDate("2024-03-06 23:59:59")); - calo.setTime(DateUtil.StringToDate("2024-02-05 00:00:00")); - System.out.println( DateUtil.calculateMonthsBetweenDates(calo,caln));*/ - -// System.out.println( -// DateUtil.DateToStr( -// DateUtil.getNextMonthFirst(new Date()) -// ,"yyyy-MM-dd HH:mm:ss") -// -// ); -// System.out.println( -// DateUtil.DateToStr( -// DateUtil.getNextMonthFirst( -// DateUtil.StringToDate("2024-12-29 21:59:59") -// ) -// ,"yyyy-MM-dd HH:mm:ss") -// -// ); - - - +// System.out.println(DateUtil.getLongDateTime(new Date()));; } + /** + * 鑾峰彇鐩稿樊鐨勬湀鏁� + * @param sDate + * @param eDate + * @return + */ + public static Integer getDifferenceMonths(Date sDate ,Date eDate) { + LocalDate startDate = sDate.toInstant() + .atZone(ZoneId.systemDefault()) + .toLocalDate(); + LocalDate endDate = eDate.toInstant() + .atZone(ZoneId.systemDefault()) + .toLocalDate(); +// LocalDate startDate= LocalDate.of(2021,2,28); +// LocalDate endDate =LocalDate.of(2022,4,5); + Period period = Period.between(endDate,startDate); + int months = period.getYears()* 12 + period.getMonths(); + return months + 1; + } + + } \ No newline at end of file -- Gitblit v1.9.3