danchaofan1412 4 лет назад
Родитель
Сommit
ca6f815e8a

+ 1 - 0
framework-boot/src/main/java/com/mrxu/framework/boot/util/DateUtil.java

@@ -11,6 +11,7 @@ import java.util.Date;
  * 日期工具类
  */
 @Slf4j
+@Deprecated
 public class DateUtil {
 
     /**

+ 410 - 0
framework-common/src/main/java/com/mrxu/framework/common/util/DateFunc.java

@@ -1,4 +1,414 @@
 package com.mrxu.framework.common.util;
 
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
+import java.util.Date;
+
 public class DateFunc {
+
+    /**
+     * 特殊时间格式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";
+
+    /**
+     * endFormat
+     */
+    public static final SimpleDateFormat END_FORMAT = new SimpleDateFormat("yyyy-MM-dd 59:59:59");
+
+    /**
+     * startFormat
+     */
+    public static final SimpleDateFormat START_FORMAT = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
+
+    /**
+     * 默认构造器
+     */
+    private DateFunc() { }
+
+    /**
+     * 获取今天0点
+     * @return String 今天
+     */
+    public static String getToday() {
+        return new SimpleDateFormat("yyyyMMdd").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("yyyyMMdd").format(time);
+    }
+
+    /**
+     * 获取今天0点
+     * @return String 今天
+     */
+    public static String getTodayStart() {
+        return START_FORMAT.format(new Date());
+    }
+
+    /**
+     * 获取今天59点
+     * @return String 今天
+     */
+    public static String getTodayEnd() {
+        return END_FORMAT.format(new Date());
+    }
+
+    /**
+     * 获取昨天
+     * @return String 昨天
+     */
+    public static String getYesterday() {
+        Calendar cal = Calendar.getInstance();
+        cal.add(Calendar.DATE, -1);
+        Date time = cal.getTime();
+        return new SimpleDateFormat("yyyy-MM-dd").format(time);
+    }
+
+    /**
+     * 获取本周的第一天
+     * @return String 本周的第一天
+     **/
+    public static String getWeekStart() {
+        Calendar cal = Calendar.getInstance();
+        //如果当天为周日,则转为上一周的周日
+        if (cal.get(Calendar.DAY_OF_WEEK) == cal.getActualMinimum(Calendar.DAY_OF_WEEK)) {
+            cal.add(Calendar.WEEK_OF_YEAR, -1);
+        }
+        cal.set(Calendar.DAY_OF_WEEK, 2);
+        Date time = cal.getTime();
+        return START_FORMAT.format(time);
+    }
+
+    /**
+     * 获取本周的最后一天
+     * @return String 本周的最后一天
+     **/
+    public static String getWeekEnd() {
+        Calendar cal = Calendar.getInstance();
+        //如果当天为非周日,则为下一周的周日
+        if (cal.get(Calendar.DAY_OF_WEEK) != cal.getActualMinimum(Calendar.DAY_OF_WEEK)) {
+            cal.add(Calendar.WEEK_OF_YEAR, 1);
+            cal.set(Calendar.DAY_OF_WEEK, cal.getActualMinimum(Calendar.DAY_OF_WEEK));
+        }
+        Date time = cal.getTime();
+        return END_FORMAT.format(time);
+    }
+
+    /**
+     * 获取本月开始日期
+     * @return String 本月开始日期
+     **/
+    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 String 次月开始日期
+     **/
+    public static String getNextMonthStart() {
+        Calendar cal = Calendar.getInstance();
+        cal.add(Calendar.MONTH, 1);
+        cal.set(Calendar.DAY_OF_MONTH, 1);
+        Date time = cal.getTime();
+        return START_FORMAT.format(time);
+    }
+
+    /**
+     * 获取次日开始日期
+     * @return String 次日开始日期
+     **/
+    public static String getNextDayStart() {
+        Calendar cal = Calendar.getInstance();
+        cal.add(Calendar.DAY_OF_MONTH, 1);
+        Date time = cal.getTime();
+        return START_FORMAT.format(time);
+    }
+
+    /**
+     * 获取本月最后一天
+     * @return String 本月最后一天
+     **/
+    public static String getMonthEnd() {
+        Calendar cal = Calendar.getInstance();
+        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
+        Date time = cal.getTime();
+        return 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);
+        Date time = cal.getTime();
+        return new SimpleDateFormat("yyyy-MM-01 00:00:00").format(time);
+    }
+
+    /**
+     * 获取本季度的最后一天
+     * @return 本季度的最后一天
+     */
+    public static String getQuarterEnd() {
+        Calendar cal = Calendar.getInstance();
+        int currentMonth = cal.get(Calendar.MONTH);
+        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 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);
+    }
+
+    /**
+     * 获取最近days日开始日期
+     * @return String 次日开始日期
+     **/
+    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);
+    }
+
+    /**
+     * 获取最近days日结束日期
+     * @return String 次日开始日期
+     **/
+    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);
+    }
+
+    /**
+     * 年月日时分秒转为年月日
+     * @param date 时间
+     * @return 转换后时间
+     */
+    public static String dateTimeToDate(Date date) {
+        return new SimpleDateFormat("yyyy-MM-dd").format(date);
+    }
+
+    /**
+     * 判断所传日期是否在今天之后
+     * @param date 判断日期
+     * @return 判断结果
+     */
+    public static boolean isAffterToday(Date date) {
+        try {
+            String todayStart = getTodayStart();
+            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+            Date todayDate = simpleDateFormat.parse(todayStart);
+            return date.after(todayDate);
+        } catch (ParseException e) {
+            throw new RuntimeException("判断所传日期是否在今天之后异常:" + e.getMessage());
+
+        }
+    }
+
+    /**
+     * 转换日期
+     *
+     * @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 日期
+     * @param format 格式
+     * @throws ParseException 解析异常
+     * @return 结果
+     */
+    public static Date getDate(String date, String format) throws ParseException {
+        SimpleDateFormat sd = new SimpleDateFormat(format);
+        return sd.parse(date);
+    }
+
+    /**
+     * 获取日期字符
+     * @param date 日期
+     * @param format 格式
+     * @return 结果
+     */
+    public static String getDateStr(Date date, String format) {
+        SimpleDateFormat sd = new SimpleDateFormat(format);
+        return sd.format(date);
+    }
+
+    /**
+     * 转换日期字符格式
+     * @param dateStr 日期
+     * @param fromFormat 原格式
+     * @param toFormat 转换后格式
+     * @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);
+    }
 }