From 02bc3bfe47e3d5311a0bb041c94e70a34b1ca73c Mon Sep 17 00:00:00 2001
From: k94314517 <8417338+k94314517@user.noreply.gitee.com>
Date: 星期二, 09 四月 2024 09:03:01 +0800
Subject: [PATCH] git ch

---
 server/service/src/main/java/com/doumee/core/utils/DateUtil.java |  221 +++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 213 insertions(+), 8 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 7942d79..893375e 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
@@ -1292,6 +1292,59 @@
         return days;
     }
 
+    public static int calculateBetween(Date newDate, Date oldDate,Integer type){
+        Calendar calo = Calendar.getInstance();
+        Calendar caln = Calendar.getInstance();
+        calo.setTime(oldDate);
+        caln.setTime(newDate);
+        if(type==0||type==1){
+            return    calculateDaysBetweenDates(caln, calo)  + 1 ;
+        }else if(type==2){
+
+            int day = caln.get(Calendar.DATE);
+            if(day == 1){
+                return    calculateMonthsBetweenDates(caln, calo) + 1;
+            }else{
+                return    calculateMonthsBetweenDates(caln, calo);
+            }
+
+        }else if(type==3){
+            return    calculateYearsBetweenDates(caln, calo) + 1;
+        }
+        return -1;
+    }
+
+
+
+    public static int calculateDaysBetweenDates(Calendar startDate, Calendar endDate) {
+        long timeDiffInMillis = Math.abs(endDate.getTimeInMillis() - startDate.getTimeInMillis());
+        return (int)(timeDiffInMillis / (24 * 60 * 60 * 1000));
+    }
+
+    public static int calculateMonthsBetweenDates(Calendar startDate, Calendar endDate) {
+        int yearDiff = endDate.get(Calendar.YEAR) - startDate.get(Calendar.YEAR);
+        int monthDiff = endDate.get(Calendar.MONTH) - startDate.get(Calendar.MONTH);
+        if (monthDiff < 0 || (monthDiff == 0 && endDate.get(Calendar.DAY_OF_MONTH) < startDate.get(Calendar.DAY_OF_MONTH))) {
+            yearDiff--;
+            monthDiff += 12;
+        }
+        return yearDiff * 12 + monthDiff;
+    }
+
+    public static int calculateYearsBetweenDates(Calendar startDate, Calendar endDate) {
+        int yearDiff = endDate.get(Calendar.YEAR) - startDate.get(Calendar.YEAR);
+        if (yearDiff > 0 && (startDate.get(Calendar.MONTH) >= endDate.get(Calendar.MONTH) ||
+                (startDate.get(Calendar.MONTH) == endDate.get(Calendar.MONTH) && startDate.get(Calendar.DAY_OF_MONTH) <= endDate.get(Calendar.DAY_OF_MONTH)))) {
+            yearDiff--;
+        } else if (yearDiff < 0 && (startDate.get(Calendar.MONTH) <= endDate.get(Calendar.MONTH) ||
+                (startDate.get(Calendar.MONTH) == endDate.get(Calendar.MONTH) && startDate.get(Calendar.DAY_OF_MONTH) >= endDate.get(Calendar.DAY_OF_MONTH)))) {
+            yearDiff++;
+        }
+        return yearDiff;
+    }
+
+
+
     /**
      * 鍙栧緱涓庡師鏃ユ湡鐩稿樊涓�瀹氬ぉ鏁扮殑鏃ユ湡锛岃繑鍥濪ate鍨嬫棩鏈�
      *
@@ -2748,13 +2801,6 @@
         return sdfLongTimePlus.parse(sdfLongTimePlus.format(date));
     }
 
-    public static void main(String[] args) {
-        try {
-            System.out.println(RandomStringUtils.randomNumeric(3));
-        } catch (Exception ex) {
-            ex.printStackTrace();
-        }
-    }
 
     /**
      *
@@ -2859,6 +2905,7 @@
         return nowDateString;
     }
 
+
     /**
      * @ author zhangyong @ 鑾峰彇褰撴湀鐨勭涓�澶╋紝2009-05-01
      */
@@ -2910,9 +2957,14 @@
         return w;
     }
 
-    public static Long twoDaysBetween(Date beginDate, Date endDate) throws ParseException {
+    public static Long twoDaysBetween(Date beginDate, Date endDate) {
         long minute = (endDate.getTime() - beginDate.getTime()) / (60 * 1000);
         return minute;
+    }
+
+    public static Integer daysBetweenDay(Date beginDate, Date endDate) {
+        long day = (endDate.getTime() - beginDate.getTime()) / (60 * 1000 * 60 * 24);
+        return Math.toIntExact(day);
     }
 
     /**
@@ -2942,6 +2994,21 @@
         calendar.add(Calendar.DATE, days);//
         return calendar.getTime();
     }
+
+    /**
+     * 鑾峰彇鎸囧畾鏃ユ湡鐨勪笅涓湀绗竴澶�
+     * @param date
+     * @return
+     */
+    public static Date getNextMonthFirst(Date date){
+        Calendar calendar = Calendar.getInstance();
+        calendar.setTime(date);
+        calendar.add(Calendar.MONTH, 1); // 灏嗘棩鍘嗙殑鏈堜唤澧炲姞1
+        calendar.set(Calendar.DAY_OF_MONTH, 1); // 璁剧疆涓轰笅涓湀鐨勭涓�澶�
+        return calendar.getTime();
+    }
+
+
 
 
     public static String afterDateToStr(Integer days){
@@ -3005,4 +3072,142 @@
         cal.add(Calendar.MINUTE,minutes);
         return cal.getTime();
     }
+
+
+    /**
+     * 璁$畻澶氬皯鑷劧骞�/鏈�/鏃ュ悗鐨勬棩鏈�
+     * @param startDate 寮�濮嬫棩鏈�
+     * @param type  0=鏃ワ紱1=鏈堬紱2=骞达紱
+     * @param cycle 鍛ㄦ湡  X鏈�
+     * @return
+     */
+    public static Date afterDateByType(Date startDate,Integer type,Integer cycle){
+        Calendar calendar = Calendar.getInstance();
+        calendar.setTime(startDate);
+        if(type==0){
+            calendar.add(Calendar.DATE, cycle);
+        }else if(type==1){
+            calendar.add(Calendar.MONTH, cycle);
+        }else if(type == 2) {
+            calendar.add(Calendar.YEAR, cycle);
+        }
+        return calendar.getTime();
+    }
+
+
+
+    public static Integer monthDays(Date date){
+        Calendar calendar = Calendar.getInstance();
+        calendar.setTime(date);
+        // 鑾峰彇褰撳墠骞翠唤銆佹湀浠斤紙娉ㄦ剰鏈堜唤浠�0寮�濮嬭绠楋級
+        int year = calendar.get(Calendar.YEAR);
+        int month = calendar.get(Calendar.MONTH) + 1;
+        // 鏍规嵁骞翠唤鍜屾湀浠藉垽鏂棸骞存儏鍐�
+        boolean isLeapYear = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
+
+        // 鏍规嵁涓嶅悓鏈堜唤璁惧畾姣忎釜鏈堢殑澶╂暟
+        switch (month) {
+            case 2:
+                if (isLeapYear) {
+                    return 29;
+                } else {
+                    return 28;
+                }
+            case 4:
+            case 6:
+            case 9:
+            case 11:
+                 return 30;
+            default:
+                 return 31;
+        }
+    }
+
+
+    /**
+     * 鎷兼帴鏃ユ湡鏁版嵁
+     * @param date
+     * @param optType   1:姣忔棩寮�濮� 2锛氭瘡鏃ョ粨鏉� 3:鏄ㄦ棩缁撴潫
+     * @return
+     */
+    public static Date getMontageDate(Date date ,Integer optType) {
+        String strShortDate = DateUtil.dateToString(date,"yyyy-MM-dd");
+        if(optType.equals(1)){
+            return DateUtil.StringToDate(strShortDate +" 00:00:00");
+        }else if(optType.equals(2)){
+            return DateUtil.StringToDate(strShortDate +" 23:59:59");
+        }else if(optType.equals(3)){
+            strShortDate = DateUtil.dateToString(DateUtil.getDateBetween(date,-1),"yyyy-MM-dd");
+            return DateUtil.StringToDate(strShortDate +" 23:59:59");
+        }else{
+            return null;
+        }
+    }
+
+
+    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"));
+
+//        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"
+//                )
+
+//        );
+//
+//        Calendar calo = Calendar.getInstance();
+//        Calendar caln = Calendar.getInstance(); // ,"yyyy-MM-dd HH:mm:ss"
+//        caln.setTime(DateUtil.StringToDate("2024-02-29 23:59:59"));
+//        calo.setTime(DateUtil.StringToDate("2024-02-01 00:00:00"));
+//        System.out.println( DateUtil.calculateDaysBetweenDates(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")
+
+        );
+
+
+
+    }
+
+
 }
\ No newline at end of file

--
Gitblit v1.9.3