|  |  | 
 |  |  | package com.moral.util; | 
 |  |  |  | 
 |  |  |  | 
 |  |  | import lombok.extern.slf4j.Slf4j; | 
 |  |  | import org.springframework.util.StringUtils; | 
 |  |  |  | 
 |  |  | import java.math.BigDecimal; | 
 |  |  | 
 |  |  | import java.time.ZonedDateTime; | 
 |  |  | import java.util.*; | 
 |  |  |  | 
 |  |  |  | 
 |  |  | @Slf4j | 
 |  |  | public class DateUtils { | 
 |  |  |     /** | 
 |  |  |      * 日期格式(yyyy) | 
 |  |  |      */ | 
 |  |  |     public static final String yyyy = "yyyy"; | 
 |  |  |     /** | 
 |  |  |      * 日期格式(yyyy-MM-dd) | 
 |  |  |      */ | 
 |  |  | 
 |  |  |      * 日期格式(yyyy-MM-dd HH) | 
 |  |  |      * */ | 
 |  |  |     public static final String yyyy_MM_dd_HH_EN = "yyyy-MM-dd HH"; | 
 |  |  |  | 
 |  |  |     /* | 
 |  |  |     * Date类toString格式 | 
 |  |  |     * */ | 
 |  |  |     public static final String EEE_MMM_dd_HH_mm_ss_zzz_yyyy = "EEE MMM dd HH:mm:ss zzz yyyy"; | 
 |  |  |     /** | 
 |  |  |      * DateFormat缓存 | 
 |  |  |      */ | 
 |  |  |     private static Map<String, DateFormat> dateFormatMap = new HashMap<String, DateFormat>(); | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |     * @Description: 将日期转换为当天的开始和结束时间 | 
 |  |  |             * @Param: [date] | 
 |  |  |             * @return: java.util.List<java.util.Date> | 
 |  |  |             * @Author: 陈凯裕 | 
 |  |  |             * @Date: 2021/9/8 | 
 |  |  |             */ | 
 |  |  |     public static List<Date> dateToStartAndEndTime(Date date){ | 
 |  |  |         String dateStr = dateToDateString(date, "yyyy-MM-dd"); | 
 |  |  |         String startDateStr = dateStr+" 00:00:00"; | 
 |  |  |         String endDateStr = dateStr+" 23:59:59"; | 
 |  |  |         Date startDate = getDate(startDateStr, "yyyy-MM-dd HH:mm:ss"); | 
 |  |  |         Date endDate = getDate(endDateStr, "yyyy-MM-dd HH:mm:ss"); | 
 |  |  |         ArrayList<Date> dates = new ArrayList<>(); | 
 |  |  |         dates.add(startDate); | 
 |  |  |         dates.add(endDate); | 
 |  |  |         return dates; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |     * @Description: Date的toString格式转为Date | 
 |  |  |             * @Param: [] | 
 |  |  |             * @return: java.util.Date | 
 |  |  |             * @Author: 陈凯裕 | 
 |  |  |             * @Date: 2021/8/25 | 
 |  |  |             */ | 
 |  |  |     public static Date dateStringToDate(String formatStr){ | 
 |  |  |         try { | 
 |  |  |             SimpleDateFormat sdf = new SimpleDateFormat(EEE_MMM_dd_HH_mm_ss_zzz_yyyy, Locale.US); | 
 |  |  |             return sdf.parse(formatStr); | 
 |  |  |         } catch (ParseException e) { | 
 |  |  |             log.error(e.getMessage()); | 
 |  |  |             return null; | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 获取DateFormat | 
 |  |  | 
 |  |  |      * @param days | 
 |  |  |      * @return | 
 |  |  |      */ | 
 |  |  |     public static String getDateAddDay(String date, int days, String format) { | 
 |  |  |         DateFormat df = getDateFormat(format); | 
 |  |  |     public static String getDateAddDay(String date, int days) { | 
 |  |  |         DateFormat df = getDateFormat(yyyy_MM_dd_EN); | 
 |  |  |         try { | 
 |  |  |             Calendar cal = Calendar.getInstance(); | 
 |  |  |             cal.setTime(df.parse(date)); | 
 |  |  | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 获取当前月第一天 | 
 |  |  |      * | 
 |  |  |      * @return | 
 |  |  |      */ | 
 |  |  |     public static Date getFirstDayOfCurrMonth() { | 
 |  |  |         Calendar cal = Calendar.getInstance(); | 
 |  |  |         cal.set(Calendar.DAY_OF_MONTH, 1); | 
 |  |  |         return getDate(dateToDateString(cal.getTime(), yyyy_MM_dd_EN)); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 获取当前月的最后一天 | 
 |  |  |      * | 
 |  |  |      * @return | 
 |  |  | 
 |  |  |         Calendar cal = Calendar.getInstance(); | 
 |  |  |         cal.add(Calendar.MONTH, 1); | 
 |  |  |         cal.set(Calendar.DAY_OF_MONTH, 0); | 
 |  |  |  | 
 |  |  |         return cal.getTime(); | 
 |  |  |         return getDate(dateToDateString(cal.getTime(), yyyy_MM_dd_EN)); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  | 
 |  |  |      * @return | 
 |  |  |      */ | 
 |  |  |     public static String getDateAddMonth(String date, int m) { | 
 |  |  |         DateFormat df = getDateFormat(yyyyMM_EN); | 
 |  |  |         DateFormat df = getDateFormat(yyyy_MM_EN); | 
 |  |  |         try { | 
 |  |  |             Calendar cal = Calendar.getInstance(); | 
 |  |  |             cal.setTime(df.parse(date)); | 
 |  |  | 
 |  |  |             throw new RuntimeException(e); | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     //获取上周一 | 
 |  |  |     public static Date getLastWeekMonday() { | 
 |  |  |         Calendar cal = Calendar.getInstance(); | 
 |  |  |         cal.setTime(getDate(getMondayOfThisWeek(), yyyy_MM_dd_EN)); | 
 |  |  |         cal.add(Calendar.DATE, -7); | 
 |  |  |         return cal.getTime(); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     //获取上月第一天 | 
 |  |  |     public static Date getFirstDayOfLastMonth() { | 
 |  |  |         Calendar calendar = Calendar.getInstance(); | 
 |  |  |         calendar.add(Calendar.MONTH, -1); | 
 |  |  |         calendar.set(Calendar.DAY_OF_MONTH, 1); | 
 |  |  |         return getDate(dateToDateString(calendar.getTime(), yyyy_MM_dd_EN)); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     /*获取指定年后年*/ | 
 |  |  |     public static String getDateAddYear(String date, int year) { | 
 |  |  |         DateFormat df = getDateFormat(yyyy); | 
 |  |  |         try { | 
 |  |  |             Calendar cal = Calendar.getInstance(); | 
 |  |  |             cal.setTime(df.parse(date)); | 
 |  |  |             cal.add(Calendar.YEAR, year); | 
 |  |  |             date = df.format(cal.getTime()); | 
 |  |  |         } catch (ParseException e) { | 
 |  |  |             throw new RuntimeException(e); | 
 |  |  |         } | 
 |  |  |         return date; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /* | 
 |  |  |      * 根据时间获取时间内时间点 | 
 |  |  |      * 例:time=2021-08-04 就得到这天内每个小时时间点2021-08-04 00,2021-08-04 01。。。 | 
 |  |  |      *    time=2021-08 就得到这月内每天时间点 | 
 |  |  |      * */ | 
 |  |  |     public static List<String> getTimeLag(String time) { | 
 |  |  |         List<String> result = new ArrayList<>(); | 
 |  |  |         int length = time.length(); | 
 |  |  |         Calendar cal = Calendar.getInstance(); | 
 |  |  |         String end; | 
 |  |  |         String dateFormat; | 
 |  |  |         String df; | 
 |  |  |         int i; | 
 |  |  |         if (length == 10) {//日 | 
 |  |  |             end = getDateAddDay(time, 1); | 
 |  |  |             dateFormat = yyyy_MM_dd_HH_EN; | 
 |  |  |             df = yyyy_MM_dd_EN; | 
 |  |  |             i = Calendar.HOUR_OF_DAY; | 
 |  |  |         } else if (length == 7) {//月 | 
 |  |  |             end = getDateAddMonth(time, 1); | 
 |  |  |             dateFormat = yyyy_MM_dd_EN; | 
 |  |  |             df = yyyy_MM_EN; | 
 |  |  |             i = Calendar.DAY_OF_MONTH; | 
 |  |  |         } else {//年 | 
 |  |  |             end = getDateAddYear(time, 1); | 
 |  |  |             dateFormat = yyyy_MM_EN; | 
 |  |  |             df = yyyy; | 
 |  |  |             i = Calendar.MONTH; | 
 |  |  |         } | 
 |  |  |         cal.setTime(getDate(time, df)); | 
 |  |  |         for (long d = cal.getTimeInMillis(); d < getDate(end, df).getTime(); cal.set(i, cal.get(i) + 1), d = cal.getTimeInMillis()) { | 
 |  |  |             String format = dateToDateString(new Date(d),dateFormat); | 
 |  |  |             result.add(format); | 
 |  |  |         } | 
 |  |  |         return result; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     //根据date取之前第一个分钟为0或5的时间点。例:2021-08-09 15:32:00->2021-08-09 15:30:00,2021-08-09 15:39:00->2021-08-09 15:35:00 | 
 |  |  |     public static Date getFiveMinuteDate(Date date) { | 
 |  |  |         String dateString = dateToDateString(date, yyyy_MM_dd_HH_mm_EN); | 
 |  |  |         String minute = dateString.substring(15, 16); | 
 |  |  |         int i = Integer.parseInt(minute); | 
 |  |  |         if (i > 0 && i < 5) { | 
 |  |  |             i = 0; | 
 |  |  |         } else if (i > 5 && i <= 9) { | 
 |  |  |             i = 5; | 
 |  |  |         } | 
 |  |  |         StringBuilder stringBuffer = new StringBuilder(dateString); | 
 |  |  |         stringBuffer.replace(15, 16, String.valueOf(i)); | 
 |  |  |         return getDate(stringBuffer.toString(), yyyy_MM_dd_HH_mm_EN); | 
 |  |  |     } | 
 |  |  | } |