From b3064052c54fa095ea0b597871f003a97c3f4c99 Mon Sep 17 00:00:00 2001
From: Your Name <you@example.com>
Date: 星期日, 05 二月 2023 13:35:22 +0800
Subject: [PATCH] 优化代码

---
 java110-utils/src/main/java/com/java110/utils/util/DateUtil.java |  299 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 289 insertions(+), 10 deletions(-)

diff --git a/java110-utils/src/main/java/com/java110/utils/util/DateUtil.java b/java110-utils/src/main/java/com/java110/utils/util/DateUtil.java
old mode 100644
new mode 100755
index 71ca605..8932162
--- a/java110-utils/src/main/java/com/java110/utils/util/DateUtil.java
+++ b/java110-utils/src/main/java/com/java110/utils/util/DateUtil.java
@@ -1,5 +1,6 @@
 package com.java110.utils.util;
 
+import java.math.BigDecimal;
 import java.sql.Timestamp;
 import java.text.DateFormat;
 import java.text.ParseException;
@@ -31,6 +32,9 @@
     public static final String DATE_FORMATE_STRING_I = "yyyy-MM-dd HH:mm:ss.SSS";
     public static final String DATE_FORMATE_STRING_J = "yyyyMMddHHmmss.SSS";
     public static final String DATE_FORMATE_STRING_K = "yyyyMMddHHmmssSSS";
+    public static final String DATE_FORMATE_STRING_L = "MMdd";
+    public static final String DATE_FORMATE_STRING_M = "yyyyMM";
+    public static final String DATE_FORMATE_STRING_N = "HHmmss";
 
     static {
         formats.put("yyyyMMddHHmmss", new SimpleDateFormat("yyyyMMddHHmmss"));
@@ -45,6 +49,9 @@
         formats.put("yyyy-MM-dd HH:mm:ss.SSS", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"));
         formats.put("yyyyMMddHHmmss.SSS", new SimpleDateFormat("yyyyMMddHHmmss.SSS"));
         formats.put("yyyyMMddHHmmssSSS", new SimpleDateFormat("yyyyMMddHHmmssSSS"));
+        formats.put("MMdd", new SimpleDateFormat("MMdd"));
+        formats.put("yyyyMM", new SimpleDateFormat("yyyyMM"));
+        formats.put("HHmmss", new SimpleDateFormat("HHmmss"));
     }
 
 
@@ -79,7 +86,7 @@
     }
 
     public static Date getLastDate() throws ParseException {
-        return getDateFromString("3000-01-01", DATE_FORMATE_STRING_B);
+        return getDateFromString("2037-12-01", DATE_FORMATE_STRING_B);
     }
 
     /**
@@ -135,6 +142,29 @@
         }
     }
 
+    public static Date getDateFromStringB(String date) {
+        SimpleDateFormat sDateFormat = getDateFormat(DateUtil.DATE_FORMATE_STRING_B);
+        try {
+            synchronized (sDateFormat) {
+                return sDateFormat.parse(date);
+            }
+        } catch (Exception e) {
+            throw new IllegalArgumentException(e);
+        }
+    }
+
+    public static Date getDateFromStringA(String date) {
+        SimpleDateFormat sDateFormat = getDateFormat(DateUtil.DATE_FORMATE_STRING_A);
+        try {
+            synchronized (sDateFormat) {
+                return sDateFormat.parse(date);
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+            throw new IllegalArgumentException(e);
+        }
+    }
+
     public static Date getDefaultDateFromString(String date)
             throws ParseException {
         return getDateFromString(date, "yyyyMMddHHmmss");
@@ -148,7 +178,8 @@
         return getFormatTimeString(new Date(), pattern);
     }
 
-    public static String getLastTime(){
+
+    public static String getLastTime() {
         return LAST_TIME;
     }
 
@@ -345,16 +376,30 @@
         return returndate;
     }
 
+    public static Date getNextMonthFirstDate() {
+        Calendar calendar = Calendar.getInstance();
+        calendar.set(Calendar.DAY_OF_MONTH, 1);
+        calendar.set(Calendar.HOUR_OF_DAY, 0);
+        calendar.set(Calendar.MINUTE, 0);
+        calendar.set(Calendar.SECOND, 0);
+        calendar.add(Calendar.MONTH, 1);
+        return calendar.getTime();
+    }
+
+    public static Date getFirstDate() {
+        Calendar curDateCal = Calendar.getInstance();
+        curDateCal.set(Calendar.DAY_OF_MONTH, 1);
+        curDateCal.set(Calendar.HOUR_OF_DAY, 0);
+        curDateCal.set(Calendar.MINUTE, 0);
+        curDateCal.set(Calendar.SECOND, 0);
+        Date curDate = curDateCal.getTime();
+        return curDate;
+    }
+
+
     public static String getNextMonthFirstDay(String fmt) {
         String returndate = "";
-        Date date = null;
-
-        Calendar cl = Calendar.getInstance();
-        cl.set(2, cl.get(2) + 1);
-        cl.set(5, 1);
-
-        date = cl.getTime();
-
+        Date date = getNextMonthFirstDate();
         returndate = getFormatTimeString(date, fmt);
 
         return returndate;
@@ -387,4 +432,238 @@
         }
         return true;
     }
+
+
+    public static int getCurrentMonthDay() {
+        Calendar a = Calendar.getInstance();
+        a.set(Calendar.DATE, 1);
+        a.roll(Calendar.DATE, -1);
+        int maxDate = a.get(Calendar.DATE);
+        return maxDate;
+    }
+
+    public static void main(String[] args) throws ParseException {
+
+//        SimpleDateFormat sf = new SimpleDateFormat(DateUtil.DATE_FORMATE_STRING_A);
+//        Calendar c = Calendar.getInstance();
+//        c.setTime(DateUtil.getDateFromString("2021-12-03",DateUtil.DATE_FORMATE_STRING_A));
+//        c.add(Calendar.DAY_OF_MONTH, 125);
+//        System.out.println("澧炲姞涓�澶╁悗鏃ユ湡:"+sf.format(c.getTime()));
+        System.out.println("2021-12-07".compareTo(DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_B)));
+    }
+
+    public static String getAddDayString(Date date, String pattern, int days) {
+        SimpleDateFormat sf = new SimpleDateFormat(pattern);
+        Calendar c = Calendar.getInstance();
+        c.setTime(date);
+        c.add(Calendar.DAY_OF_MONTH, days);
+        return sf.format(c.getTime());
+    }
+
+    public static String getAddDayStringB(Date date, int days) {
+        SimpleDateFormat sf = new SimpleDateFormat(DATE_FORMATE_STRING_B);
+        Calendar c = Calendar.getInstance();
+        c.setTime(date);
+        c.add(Calendar.DAY_OF_MONTH, days);
+        return sf.format(c.getTime());
+    }
+
+    public static String getAddDayStringA(Date date, int days) {
+        SimpleDateFormat sf = new SimpleDateFormat(DATE_FORMATE_STRING_A);
+        Calendar c = Calendar.getInstance();
+        c.setTime(date);
+        c.add(Calendar.DAY_OF_MONTH, days);
+        return sf.format(c.getTime());
+    }
+
+
+    public static String getAddMonthStringA(Date date, int month) {
+        SimpleDateFormat sf = new SimpleDateFormat(DATE_FORMATE_STRING_A);
+        Calendar c = Calendar.getInstance();
+        c.setTime(date);
+        c.add(Calendar.MONTH, month);
+        return sf.format(c.getTime());
+    }
+
+    /**
+     * 鍦ㄧ粰瀹氱殑鏃ユ湡鍔犱笂鎴栧噺鍘绘寚瀹氭湀浠藉悗鐨勬棩鏈�
+     *
+     * @param sourceDate 鍘熷鏃堕棿
+     * @param month      瑕佽皟鏁寸殑鏈堜唤锛屽悜鍓嶄负璐熸暟锛屽悜鍚庝负姝f暟
+     * @return
+     */
+    public static Date stepMonth(Date sourceDate, int month) {
+        Calendar c = Calendar.getInstance();
+        c.setTime(sourceDate);
+        c.add(Calendar.MONTH, month);
+
+        return c.getTime();
+    }
+
+    /**
+     * 鍦ㄧ粰瀹氱殑鏃ユ湡鍔犱笂鎴栧噺鍘绘寚瀹氬ぉ鏁板悗鐨勬棩鏈�
+     *
+     * @param sourceDate 鍘熷鏃堕棿
+     * @param day        瑕佽皟鏁寸殑鏈堜唤锛屽悜鍓嶄负璐熸暟锛屽悜鍚庝负姝f暟
+     * @return
+     */
+    public static Date stepDay(Date sourceDate, int day) {
+        Calendar c = Calendar.getInstance();
+        c.setTime(sourceDate);
+        c.add(Calendar.DATE, day);
+        return c.getTime();
+    }
+
+    public static String dateTimeToDate(String dateTime) {
+        String dateStr = "";
+        try {
+            Date date = getDateFromString(dateTime, DATE_FORMATE_STRING_A);
+            dateStr = getFormatTimeString(date, DATE_FORMATE_STRING_B);
+        } catch (ParseException e) {
+            dateStr = dateTime;
+        }
+
+        return dateStr;
+    }
+
+
+    public static int getYear() {
+        Date date = getCurrentDate();
+        Calendar calendar = Calendar.getInstance();
+        calendar.setTime(date);
+        return calendar.get(Calendar.YEAR);
+    }
+
+    public static int getMonth() {
+        Date date = getCurrentDate();
+        Calendar calendar = Calendar.getInstance();
+        calendar.setTime(date);
+        return calendar.get(Calendar.MONTH) + 1;
+    }
+
+    /**
+     * 鍒ゆ柇鏃堕棿鏄惁鍦ㄦ椂闂存鍐�
+     *
+     * @param nowTime
+     * @param beginTime
+     * @param endTime
+     * @return
+     */
+    public static boolean belongCalendar(Date nowTime, Date beginTime, Date endTime) {
+        Calendar date = Calendar.getInstance();
+        date.setTime(nowTime);
+        Calendar begin = Calendar.getInstance();
+        begin.setTime(beginTime);
+        Calendar end = Calendar.getInstance();
+        end.setTime(endTime);
+        if (date.after(begin) && date.before(end)) {
+            return true;
+        } else if (nowTime.compareTo(beginTime) == 0 || nowTime.compareTo(endTime) == 0) {
+            return true;
+        } else {
+            return false;
+        }
+    }
+
+    //鑾峰彇涓や釜鏃ユ湡涔嬮棿鐨勫ぉ鏁�
+    public static int daysBetween(Date now, Date returnDate) {
+        Calendar cNow = Calendar.getInstance();
+        Calendar cReturnDate = Calendar.getInstance();
+        cNow.setTime(now);
+        cReturnDate.setTime(returnDate);
+        setTimeToMidnight(cNow);
+        setTimeToMidnight(cReturnDate);
+        long todayMs = cNow.getTimeInMillis();
+        long returnMs = cReturnDate.getTimeInMillis();
+        long intervalMs = todayMs - returnMs;
+        return millisecondsToDays(intervalMs);
+    }
+
+    //鑾峰彇涓や釜鏃ユ湡涔嬮棿鐨勬绉掓暟
+    private static void setTimeToMidnight(Calendar calendar) {
+        calendar.set(Calendar.HOUR_OF_DAY, 0);
+        calendar.set(Calendar.MINUTE, 0);
+        calendar.set(Calendar.SECOND, 0);
+    }
+
+    //鑾峰彇涓や釜鏃ユ湡涔嬮棿鐨勫垎閽熸暟
+    private static int millisecondsToDays(long intervalMs) {
+        return (int) (intervalMs / (1000 * 86400));
+    }
+
+    /**
+     * 銆�銆� *瀛楃涓茬殑鏃ユ湡鏍煎紡鐨勮绠�
+     *
+     */
+    public static int daysBetween(String smdate, String bdate) {
+        long between_days = 0;
+        try {
+            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
+            Calendar cal = Calendar.getInstance();
+            cal.setTime(sdf.parse(smdate));
+            long time1 = cal.getTimeInMillis();
+            cal.setTime(sdf.parse(bdate));
+            long time2 = cal.getTimeInMillis();
+            between_days = (time2 - time1) / (1000 * 3600 * 24);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+
+        return Integer.parseInt(String.valueOf(between_days));
+    }
+
+    public static double dayCompare(Date fromDate, Date toDate) {
+        double resMonth = 0.0;
+        Calendar from = Calendar.getInstance();
+        from.setTime(fromDate);
+        Calendar to = Calendar.getInstance();
+        to.setTime(toDate);
+        //姣旇緝鏈堜唤宸� 鍙兘鏈夋暣鏁� 涔熶細璐熸暟
+        int result = to.get(Calendar.MONTH) - from.get(Calendar.MONTH);
+        //姣旇緝骞村樊
+        int month = (to.get(Calendar.YEAR) - from.get(Calendar.YEAR)) * 12;
+
+        //鐪熷疄 鐩稿樊鏈堜唤
+        result = result + month;
+
+        //寮�濮嬫椂闂�  2021-06-01  2021-08-05   result = 2    2021-08-01
+        Calendar newFrom = Calendar.getInstance();
+        newFrom.setTime(fromDate);
+        newFrom.add(Calendar.MONTH, result);
+        //濡傛灉鍔犳湀浠藉悗 澶т簬浜嗗綋鍓嶆椂闂� 榛樿鍔� 鏈堜唤 -1 鎯呭喌 12-19  21-01-10
+        //杩欎釜鏄鐨勯�昏緫涓�瀹氬ソ濂界悊瑙�
+        if (newFrom.getTime().getTime() > toDate.getTime()) {
+            newFrom.setTime(fromDate);
+            result = result - 1;
+            newFrom.add(Calendar.MONTH, result);
+        }
+
+        // t1 2021-08-01   t2 2021-08-05
+        long t1 = newFrom.getTime().getTime();
+        long t2 = to.getTime().getTime();
+        //鐩稿樊姣
+        double days = (t2 - t1) * 1.00 / (24 * 60 * 60 * 1000);
+        BigDecimal tmpDays = new BigDecimal(days); //鐩稿樊澶╂暟
+        BigDecimal monthDay = null;
+        Calendar newFromMaxDay = Calendar.getInstance();
+        newFromMaxDay.set(newFrom.get(Calendar.YEAR), newFrom.get(Calendar.MONTH), 1, 0, 0, 0);
+        newFromMaxDay.add(Calendar.MONTH, 1); //涓嬩釜鏈�1鍙�
+        //鍦ㄥ綋鍓嶆湀涓� 杩欏潡鏈夐棶棰�
+        if (toDate.getTime() < newFromMaxDay.getTime().getTime()) {
+            monthDay = new BigDecimal(newFrom.getActualMaximum(Calendar.DAY_OF_MONTH));
+            return tmpDays.divide(monthDay, 4, BigDecimal.ROUND_HALF_UP).add(new BigDecimal(result)).doubleValue();
+        }
+        // 涓婃湀澶╂暟
+        days = (newFromMaxDay.getTimeInMillis() - t1) * 1.00 / (24 * 60 * 60 * 1000);
+        tmpDays = new BigDecimal(days);
+        monthDay = new BigDecimal(newFrom.getActualMaximum(Calendar.DAY_OF_MONTH));
+        BigDecimal preRresMonth = tmpDays.divide(monthDay, 4, BigDecimal.ROUND_HALF_UP);
+
+        //涓嬫湀澶╂暟
+        days = (t2 - newFromMaxDay.getTimeInMillis()) * 1.00 / (24 * 60 * 60 * 1000);
+        tmpDays = new BigDecimal(days);
+        monthDay = new BigDecimal(newFromMaxDay.getActualMaximum(Calendar.DAY_OF_MONTH));
+        resMonth = tmpDays.divide(monthDay, 4, BigDecimal.ROUND_HALF_UP).add(new BigDecimal(result)).add(preRresMonth).doubleValue();
+        return resMonth;
+    }
 }

--
Gitblit v1.8.0