Procházet zdrojové kódy

日期工具类完善

xujunwei před 3 roky
rodič
revize
0510cf8982

+ 119 - 230
framework-common/src/main/java/com/mrxu/framework/common/util/DateFunc.java

@@ -7,86 +7,47 @@ import java.util.Date;
 
 public class DateFunc {
 
-    public static final String DAY_FORMAT = "yyyy-MM-dd";
-
     public static final String TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
+    public static final String DAY_FORMAT = "yyyy-MM-dd";
+    public static final String MONTH_FORMAT = "yyyy-MM";
+    public static final String YEAR_FORMAT = "yyyy";
 
-    /**
-     * 特殊时间格式1
-     */
-    public static final String ZONE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss.SSSZ";
-
-    /**
-     * 特殊时间格式2
-     */
-    public static final String SEPECIAL_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ssZ";
+    public static final String TIME_START_FORMAT = "yyyy-MM-dd 00:00:00";
+    public static final String TIME_END_FORMAT = "yyyy-MM-dd 23:59:59";
 
-    /**
-     * @Author: xujunwei
-     * @Date: 2021/12/7
-     * @Description: 定义永久时间 :2099-09-09日
-     */ 
+    // 定义永久时间 :2099-09-09
     public static final Date FOREVER_DATE = new Date(4092566400000l);
 
-    public static final SimpleDateFormat FORMAT = new SimpleDateFormat(TIME_FORMAT);
+    // 单利模式构造方法私有
+    private DateFunc() {}
 
-    /**
-     * 时间格式
-     */
-    public static final SimpleDateFormat dayFormat = new SimpleDateFormat(DAY_FORMAT);
-
-    /**
-     * endFormat
-     */
-    public static final SimpleDateFormat END_FORMAT = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
-
-    /**
-     * startFormat
-     */
-    public static final SimpleDateFormat START_FORMAT = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
+    // 获取本年
+    public static String getYear() {
+        return new SimpleDateFormat(YEAR_FORMAT).format(new Date());
+    }
 
-    /**
-     * 默认构造器
-     */
-    private DateFunc() { }
+    // 获取本月
+    public static String getMonth() {
+        return new SimpleDateFormat(MONTH_FORMAT).format(new Date());
+    }
 
-    /**
-     * 获取今天0点
-     * @return String 今天
-     */
+    // 当天日期
     public static String getToday() {
-        return new SimpleDateFormat("yyyy-MM-dd").format(new Date());
+        return new SimpleDateFormat(DAY_FORMAT).format(new Date());
     }
 
+    // 当前时间
     public static String getNow() {
         return new SimpleDateFormat(TIME_FORMAT).format(new Date());
     }
 
-    /**
-     * 获取本年
-     * @return
-     */
-    public static String getYear() {
-        return new SimpleDateFormat("yyyy").format(new Date());
-    }
-
-    /**
-     * 获取昨天
-     * @return String 昨天
-     */
-    public static String getYesterdayStr() {
-        Calendar cal = Calendar.getInstance();
-        cal.add(Calendar.DATE, -1);
-        Date time = cal.getTime();
-        return new SimpleDateFormat("yyyy-MM-dd").format(time);
-    }
 
     /**
      * 获取今天0点
      * @return String 今天
      */
     public static String getTodayStart() {
-        return START_FORMAT.format(new Date());
+        return new SimpleDateFormat(TIME_START_FORMAT).format(new Date());
     }
 
     /**
@@ -94,7 +55,7 @@ public class DateFunc {
      * @return String 今天
      */
     public static String getTodayEnd() {
-        return END_FORMAT.format(new Date());
+        return new SimpleDateFormat(TIME_START_FORMAT).format(new Date());
     }
 
     /**
@@ -112,10 +73,23 @@ public class DateFunc {
         return getYesterday()+" 00:00:00";
     }
 
+    public static String getYesterdayEnd() {
+        return getYesterday()+" 23:59:59";
+    }
+
     /**
-     * 获取本周的第一天
-     * @return String 本周的第一天
+     * 获取次日开始时刻
      **/
+    public static String getNextDayStart() {
+        Calendar cal = Calendar.getInstance();
+        cal.add(Calendar.DAY_OF_MONTH, 1);
+        Date time = cal.getTime();
+        return new SimpleDateFormat(TIME_START_FORMAT).format(time);
+    }
+
+    /**
+     * 获取本周的时刻
+     */
     public static String getWeekStart() {
         Calendar cal = Calendar.getInstance();
         //如果当天为周日,则转为上一周的周日
@@ -124,12 +98,11 @@ public class DateFunc {
         }
         cal.set(Calendar.DAY_OF_WEEK, 2);
         Date time = cal.getTime();
-        return START_FORMAT.format(time);
+        return new SimpleDateFormat(TIME_START_FORMAT).format(time);
     }
 
     /**
-     * 获取本周的最后一天
-     * @return String 本周的最后一天
+     * 获取本周结束的时刻
      **/
     public static String getWeekEnd() {
         Calendar cal = Calendar.getInstance();
@@ -139,70 +112,88 @@ public class DateFunc {
             cal.set(Calendar.DAY_OF_WEEK, cal.getActualMinimum(Calendar.DAY_OF_WEEK));
         }
         Date time = cal.getTime();
-        return END_FORMAT.format(time);
+        return new SimpleDateFormat(TIME_END_FORMAT).format(time);
     }
 
     /**
-     * 获取本月开始日期
-     * @return String 本月开始日期
+     * 获取上月月开始时刻
+     **/
+    public static String getPreMonthStart() {
+        Calendar cal = Calendar.getInstance();
+        cal.add(Calendar.MONTH, -1);
+        cal.set(Calendar.DAY_OF_MONTH, 1);
+        Date time = cal.getTime();
+        return new SimpleDateFormat(TIME_START_FORMAT).format(time);
+    }
+
+    /**
+     * 获取上月最后时刻
+     **/
+    public static String getPreMonthEnd() {
+        Calendar cal = Calendar.getInstance();
+        cal.add(Calendar.MONTH, -1);
+        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
+        Date time = cal.getTime();
+        return new SimpleDateFormat(TIME_END_FORMAT).format(time);
+    }
+
+    /**
+     * 获取本月开始时刻
      **/
     public static String getMonthStart() {
         Calendar cal = Calendar.getInstance();
         cal.add(Calendar.MONTH, 0);
         cal.set(Calendar.DAY_OF_MONTH, 1);
         Date time = cal.getTime();
-        return START_FORMAT.format(time);
+        return new SimpleDateFormat(TIME_START_FORMAT).format(time);
     }
 
     /**
-     * 获取次月开始日期
-     * @return String 次月开始日期
+     * 获取本月最后时刻
      **/
-    public static String getNextMonthStart() {
+    public static String getMonthEnd() {
         Calendar cal = Calendar.getInstance();
-        cal.add(Calendar.MONTH, 1);
-        cal.set(Calendar.DAY_OF_MONTH, 1);
+        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
         Date time = cal.getTime();
-        return START_FORMAT.format(time);
+        return new SimpleDateFormat(TIME_END_FORMAT).format(time);
     }
 
     /**
-     * 获取次日开始日期
-     * @return String 次日开始日期
+     * 获取次月开始时刻
      **/
-    public static String getNextDayStart() {
+    public static String getNextMonthStart() {
         Calendar cal = Calendar.getInstance();
-        cal.add(Calendar.DAY_OF_MONTH, 1);
+        cal.add(Calendar.MONTH, 1);
+        cal.set(Calendar.DAY_OF_MONTH, 1);
         Date time = cal.getTime();
-        return START_FORMAT.format(time);
+        return new SimpleDateFormat(TIME_START_FORMAT).format(time);
     }
 
     /**
-     * 获取本月最后一天
-     * @return String 本月最后一天
+     * 获取下月最后时刻
      **/
-    public static String getMonthEnd() {
+    public static String getNextMonthEnd() {
         Calendar cal = Calendar.getInstance();
+        cal.add(Calendar.MONTH, 1);
         cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
         Date time = cal.getTime();
-        return END_FORMAT.format(time);
+        return new SimpleDateFormat(TIME_END_FORMAT).format(time);
     }
 
     /**
-     * 获取本季度第一天
-     * @return 本季度第一天
+     * 获取本季度开始时刻
      */
     public static String getQuarterStart() {
         Calendar cal = Calendar.getInstance();
         int currentMonth = cal.get(Calendar.MONTH);
         cal.set(Calendar.MONTH, currentMonth - currentMonth % 3);
+        cal.set(Calendar.DAY_OF_MONTH, 1);
         Date time = cal.getTime();
-        return new SimpleDateFormat("yyyy-MM-01 00:00:00").format(time);
+        return new SimpleDateFormat(TIME_START_FORMAT).format(time);
     }
 
     /**
-     * 获取本季度的最后一天
-     * @return 本季度的最后一天
+     * 获取本季度的最后时刻
      */
     public static String getQuarterEnd() {
         Calendar cal = Calendar.getInstance();
@@ -210,70 +201,53 @@ public class DateFunc {
         cal.set(Calendar.MONTH, currentMonth + 2 - currentMonth % 3);
         cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
         Date time = cal.getTime();
-        return END_FORMAT.format(time);
+        return new SimpleDateFormat(TIME_END_FORMAT).format(time);
     }
 
     /**
-     * 获取本年的第一天
-     * @return String 本年的第一天
+     * 获取本年的开始时刻
      **/
     public static String getYearStart() {
         return new SimpleDateFormat("yyyy-01-01 00:00:00").format(new Date());
     }
 
     /**
-     * 获取本年的最后一天
-     * @return String 本年的最后一天
+     * 获取本年的最后时刻
      **/
     public static String getYearEnd() {
         Calendar calendar = Calendar.getInstance();
         calendar.set(Calendar.MONTH, calendar.getActualMaximum(Calendar.MONTH));
         calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
         Date currYearLast = calendar.getTime();
-        return END_FORMAT.format(currYearLast);
-    }
-
-    /**
-     * 获取本月的最后一天
-     * @param dateTime 时间
-     * @return String 本月的最后一天
-     **/
-    public static String dateTimeToDate(String dateTime) {
-        Calendar calendar = Calendar.getInstance();
-        calendar.set(Calendar.MONTH, calendar.getActualMaximum(Calendar.MONTH));
-        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
-        Date currYearLast = calendar.getTime();
-        return END_FORMAT.format(currYearLast);
+        return new SimpleDateFormat(TIME_END_FORMAT).format(currYearLast);
     }
 
     /**
-     * 获取最近days日开始日期
-     * @return String 次日开始日期
+     * 获取最近days日开始时刻
      **/
     public static String getLastNDayStart(int days) {
         Calendar cal = Calendar.getInstance();
         cal.add(Calendar.DAY_OF_MONTH, days);
         Date time = cal.getTime();
-        return START_FORMAT.format(time);
+        return new SimpleDateFormat(TIME_START_FORMAT).format(time);
     }
 
     /**
-     * 获取最近days日结束日期
-     * @return String 次日开始日期
+     * 获取最近days日结束时刻
      **/
     public static String getLastNDayEnd(int days) {
         Calendar cal = Calendar.getInstance();
         cal.add(Calendar.DAY_OF_MONTH, days);
         Date time = cal.getTime();
-        return END_FORMAT.format(time);
+        return new SimpleDateFormat(TIME_END_FORMAT).format(time);
     }
 
     /**
      * 获取最近days日结束日期
-     * @return String 次日开始日期
      **/
     public static Date getLastDayEnd(int days) {
         try {
+            SimpleDateFormat FORMAT = new SimpleDateFormat(TIME_FORMAT);
             return FORMAT.parse(getLastNDayEnd(days));
         } catch (ParseException e) {
             e.printStackTrace();
@@ -294,10 +268,6 @@ public class DateFunc {
         }
     }
 
-    public static void main(String[] args) {
-        System.out.println(getLastDayEnd(23));
-    }
-
     /**
      * 年月日时分秒转为年月日
      * @param date 时间
@@ -324,92 +294,6 @@ public class DateFunc {
         }
     }
 
-    /**
-     * 转换日期
-     *
-     * @param zoneTime 时间字符串
-     * @return 转换结果
-     */
-    public static Date parseZoneTimeStr(String zoneTime) {
-        try {
-            SimpleDateFormat sd1 = new SimpleDateFormat(ZONE_TIME_FORMAT);
-            Date dt = sd1.parse(zoneTime);
-            return dt;
-        } catch (ParseException e) {
-            throw new RuntimeException("转换异常");
-        }
-    }
-
-    /**
-     * 转换日期
-     *
-     * @param date 日期 yyyyMMdd
-     * @return 转换结果
-     */
-    public static String getSepecialTime(String date) {
-        try {
-            SimpleDateFormat sd1 = new SimpleDateFormat("yyyyMMdd");
-            Date dt = sd1.parse(date);
-
-            SimpleDateFormat sd2 = new SimpleDateFormat(SEPECIAL_TIME_FORMAT);
-            return sd2.format(dt);
-        } catch (ParseException e) {
-            throw new RuntimeException("转换异常");
-        }
-    }
-
-    /**
-     * 获取下一天
-     * @param date 日期 yyyyMMdd
-     * @return String 昨天
-     */
-    public static String getNextDay(String date) {
-        try {
-            SimpleDateFormat sd = new SimpleDateFormat("yyyyMMdd");
-            Date dt = sd.parse(date);
-            Calendar cal = Calendar.getInstance();
-            cal.setTime(dt);
-            cal.add(Calendar.DATE, 1);
-            Date time = cal.getTime();
-            return sd.format(time);
-        } catch (ParseException e) {
-            throw new RuntimeException("转换异常");
-        }
-    }
-
-    /**
-     * 转换日期
-     * @param date 日期
-     * @return 转换结果
-     */
-    public static String getSepecialTime(Date date) {
-        SimpleDateFormat sd1 = new SimpleDateFormat(SEPECIAL_TIME_FORMAT);
-        return sd1.format(date);
-    }
-
-    /**
-     * 日期
-     * @param date 日期
-     * @param differTime 取差时间数
-     * @param differType 取差时间类型 1-小时 2-分钟 3-秒 4-天
-     * @return 转换结果
-     */
-    public static Date getDifferTime(Date date, Long differTime, Integer differType) {
-        Long time = date.getTime();
-        if (1 == differType) {
-            time = time - differTime * 60 * 60 * 1000L;
-        } else if (2 == differType) {
-            time = time - differTime * 60 * 1000L;
-        } else if (3 == differType) {
-            time = time - differTime * 1000L;
-        } else if (4 == differType) {
-            time = time - differTime * 24 * 60 * 60 * 1000L;
-        }
-        Date diffDate = new Date();
-        diffDate.setTime(time);
-        return diffDate;
-    }
-
     /**
      * 获取日期
      * @param date 日期
@@ -445,31 +329,15 @@ public class DateFunc {
      * @throws ParseException 解析异常
      * @return 结果
      */
-    public static String transDateFormat(String dateStr, String fromFormat, String toFormat) throws ParseException {
-        SimpleDateFormat sd1 = new SimpleDateFormat(fromFormat);
-        SimpleDateFormat sd2 = new SimpleDateFormat(toFormat);
-        Date date = sd1.parse(dateStr);
-        return sd2.format(date);
-    }
-
-
-    /**
-     * 获取本月开始日期
-     * @param dateStr 日期
-     * @param dateFormat 日期格式
-     * @param resultFormat 结果格式
-     * @throws ParseException 解析异常
-     * @return String 月开始日期
-     **/
-    public static String getMonthStart(String dateStr, String dateFormat, String resultFormat) throws ParseException {
-        SimpleDateFormat sd1 = new SimpleDateFormat(dateFormat);
-        Date date = sd1.parse(dateStr);
-        Calendar cal = Calendar.getInstance();
-        cal.setTime(date);
-        cal.add(Calendar.MONTH, 0);
-        cal.set(Calendar.DAY_OF_MONTH, 1);
-        Date time = cal.getTime();
-        return new SimpleDateFormat(resultFormat).format(time);
+    public static String transDateFormat(String dateStr, String fromFormat, String toFormat) {
+        try {
+            SimpleDateFormat sd1 = new SimpleDateFormat(fromFormat);
+            SimpleDateFormat sd2 = new SimpleDateFormat(toFormat);
+            Date date = sd1.parse(dateStr);
+            return sd2.format(date);
+        } catch (ParseException e) {
+            throw new BusinessException("日期格式错误");
+        }
     }
 
     public static String getDayStr(Date date) {
@@ -480,4 +348,25 @@ public class DateFunc {
         return getDateStr(date,TIME_FORMAT);
     }
 
+    // 验证 SimpleDateFormat 线程非安全
+    private static SimpleDateFormat sdt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+    public static void main(String[] args) {
+        Date FOREVER_DATE = new Date(4092652799999L); // 2099-09-09 23:59:59
+        Date NOW = new Date(1652429483663L); // 2022-05-13 16:11:23
+        for(int i=0;i<10;i++) {
+            new Thread(new Runnable() {
+                public void run() {
+                    String foreverStr = sdt.format(FOREVER_DATE);
+                    if(!"2099-09-09 23:59:59".equals(foreverStr)) {
+                        System.out.println("forever error :"+foreverStr);
+                    }
+                    String nowStr = sdt.format(NOW);
+                    if(!"2022-05-13 16:11:23".equals(nowStr)) {
+                        System.out.println("now error :"+nowStr);
+                    }
+                }
+            }).start();
+        }
+    }
+
 }