|
@@ -0,0 +1,487 @@
|
|
|
+package com.persagy.apm.energyalarmstarter.alarmdata.utils;
|
|
|
+
|
|
|
+import com.google.common.collect.Lists;
|
|
|
+
|
|
|
+import java.text.ParseException;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.time.Instant;
|
|
|
+import java.time.LocalDate;
|
|
|
+import java.time.ZoneId;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+public class DateUtils {
|
|
|
+ public static final String SDFSECOND = "yyyyMMddHHmmss";
|
|
|
+ public static final String SDFDAY = "yyyyMMdd";
|
|
|
+ public static final String SDF_SECOND = "yyyy-MM-dd HH:mm:ss";
|
|
|
+ public static final String SDF_SPACE_SECOND = "yyyyMMdd HH:mm:ss";
|
|
|
+ public static final String SDF_DOT_DAY = "yyyy.MM.dd";
|
|
|
+ public static final String SDF_CHAR_MONTH_DAY = "MM月dd日";
|
|
|
+
|
|
|
+
|
|
|
+ * 将pattern格式的字符串日期转换为Date类型日期
|
|
|
+ *
|
|
|
+ * @param source pattern格式的字符串日期
|
|
|
+ * @param pattern 日期格式如yyyy-MM-dd HH:mm:ss
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static Date str2Date(String source, String pattern) throws Exception {
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat(pattern);
|
|
|
+ return sdf.parse(source);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 将Date类型的日期转换成pattern格式的字符串日期
|
|
|
+ *
|
|
|
+ * @param date
|
|
|
+ * @param pattern
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static String date2Str(Date date, String pattern) {
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat(pattern);
|
|
|
+ return sdf.format(date);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 获取时间按年偏移offset个单位后的时间
|
|
|
+ *
|
|
|
+ * @param date
|
|
|
+ * @param offset
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date getYearOff(Date date, int offset) {
|
|
|
+ return getOffset(date, offset, 5);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 获取时间按月偏移offset个单位后的时间
|
|
|
+ *
|
|
|
+ * @param date
|
|
|
+ * @param offset
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date getMonthOff(Date date, int offset) {
|
|
|
+ return getOffset(date, offset, 4);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 获取时间按周偏移offset个单位后的时间
|
|
|
+ *
|
|
|
+ * @param date
|
|
|
+ * @param offset
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date getWeekOff(Date date, int offset) {
|
|
|
+ return getOffset(date, offset, 3);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 获取时间按日偏移offset个单位后的时间
|
|
|
+ *
|
|
|
+ * @param date
|
|
|
+ * @param offset
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date getDayOff(Date date, int offset) {
|
|
|
+ return getOffset(date, offset, 2);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 获取时间按小时偏移offset个单位后的时间
|
|
|
+ *
|
|
|
+ * @param date
|
|
|
+ * @param offset
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date getHourOff(Date date, int offset) {
|
|
|
+ return getOffset(date, offset, 1);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * description 获取时间按分钟偏移offset个单位后的时间
|
|
|
+ *
|
|
|
+ * @param date
|
|
|
+ * @param offset
|
|
|
+ * @return java.util.Date
|
|
|
+ * @author feng
|
|
|
+ * @since 18:51 2018/12/10
|
|
|
+ */
|
|
|
+ public static Date getMinuteOff(Date date, int offset) {
|
|
|
+ return getOffset(date, offset, 0);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 获取指定时间粒度偏移量偏移后的时间
|
|
|
+ *
|
|
|
+ * @param date
|
|
|
+ * @param offset int 偏移量
|
|
|
+ * @param type int 时间粒度0-分,1-时,2-天,3-周,4-月,5-年
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private static Date getOffset(Date date, int offset, int type) {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(date);
|
|
|
+ switch (type) {
|
|
|
+ case 0:
|
|
|
+ cal.add(Calendar.MINUTE, offset);
|
|
|
+ break;
|
|
|
+ case 1:
|
|
|
+ cal.add(Calendar.HOUR_OF_DAY, offset);
|
|
|
+ break;
|
|
|
+ case 2:
|
|
|
+ cal.add(Calendar.DAY_OF_MONTH, offset);
|
|
|
+ break;
|
|
|
+ case 3:
|
|
|
+ cal.add(Calendar.DAY_OF_MONTH, offset * 7);
|
|
|
+ break;
|
|
|
+ case 4:
|
|
|
+ cal.add(Calendar.MONTH, offset);
|
|
|
+ break;
|
|
|
+ case 5:
|
|
|
+ cal.add(Calendar.YEAR, offset);
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ return cal.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 获取某一天的开始时间 即2017-01-01 00:00:00
|
|
|
+ *
|
|
|
+ * @param date 时间参数
|
|
|
+ * @return
|
|
|
+ * @throws ParseException
|
|
|
+ */
|
|
|
+ public static Date getStartTimeOfDay(Date date) throws ParseException {
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+ c.setTime(date);
|
|
|
+ int year = c.get(Calendar.YEAR);
|
|
|
+ int month = c.get(Calendar.MONTH);
|
|
|
+ int day = c.get(Calendar.DAY_OF_MONTH);
|
|
|
+ c.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
+ c.set(Calendar.MINUTE, 0);
|
|
|
+ c.set(Calendar.SECOND, 0);
|
|
|
+ c.set(Calendar.MILLISECOND, 0);
|
|
|
+ c.set(Calendar.DAY_OF_MONTH, day);
|
|
|
+ c.set(Calendar.YEAR, year);
|
|
|
+ c.set(Calendar.MONTH, month);
|
|
|
+ return c.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Date getFirstDayOfYear(Date date) {
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+ c.setTime(date);
|
|
|
+ int year = c.get(Calendar.YEAR);
|
|
|
+ c.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
+ c.set(Calendar.DAY_OF_MONTH, 1);
|
|
|
+ c.set(Calendar.MINUTE, 0);
|
|
|
+ c.set(Calendar.SECOND, 0);
|
|
|
+ c.set(Calendar.MILLISECOND, 0);
|
|
|
+ c.set(Calendar.YEAR, year);
|
|
|
+ c.set(Calendar.MONTH, 0);
|
|
|
+ return c.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 去年的第一天
|
|
|
+ *
|
|
|
+ * @param date 参照的日期
|
|
|
+ * @return 去年的第一天
|
|
|
+ * @author lixing
|
|
|
+ * @version V1.0 2021/5/30 11:43 上午
|
|
|
+ */
|
|
|
+ public static Date getFirstDayOfLastYear(Date date) {
|
|
|
+ if (date == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ Instant instant = date.toInstant();
|
|
|
+ ZoneId zoneId = ZoneId.systemDefault();
|
|
|
+
|
|
|
+ LocalDate localDate = instant.atZone(zoneId).toLocalDate();
|
|
|
+ LocalDate lastYear = localDate.minusYears(1);
|
|
|
+ LocalDate lastYearFirstDay = LocalDate.of(lastYear.getYear(), 1, 1);
|
|
|
+ return Date.from(lastYearFirstDay.atStartOfDay(zoneId).toInstant());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 获取两个时间的间隔(ms) endDate - beginDate
|
|
|
+ *
|
|
|
+ * @param beginDate 开始时间
|
|
|
+ * @param endDate 结束时间
|
|
|
+ * @return 相差的时间间隔(ms)
|
|
|
+ * @author lixing
|
|
|
+ * @version V1.0 2021/9/1 10:17 上午
|
|
|
+ */
|
|
|
+ public static Double getDateDurationMs(Date beginDate, Date endDate) {
|
|
|
+ long diff = endDate.getTime() - beginDate.getTime();
|
|
|
+ return (double) diff;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Double getDateDuration(Date beginDate, Date endDate) {
|
|
|
+ long nh = 1000 * 60 * 60;
|
|
|
+ long diff = endDate.getTime() - beginDate.getTime();
|
|
|
+ return (double) diff / nh;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Double getDateDuration(long beginTime, long endTime) {
|
|
|
+ long nh = 1000 * 60 * 60;
|
|
|
+ long diff = endTime - beginTime;
|
|
|
+ return (double) diff / nh;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static List<Date> getDayList(Date startDate, Date endDate) throws Exception {
|
|
|
+ List<Date> resultList = new ArrayList<>();
|
|
|
+ while (startDate.before(endDate)) {
|
|
|
+ resultList.add(startDate);
|
|
|
+ startDate = getDayOff(startDate, 1);
|
|
|
+ }
|
|
|
+ return resultList;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * date转换为localDate
|
|
|
+ *
|
|
|
+ * @param date Date对象
|
|
|
+ * @return LocalDate对象
|
|
|
+ * @author lixing
|
|
|
+ * @version V1.0 2021/6/3 11:36 上午
|
|
|
+ */
|
|
|
+ public static LocalDate convert2LocalDate(Date date) {
|
|
|
+ if (date == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ Instant instant = date.toInstant();
|
|
|
+ ZoneId zoneId = ZoneId.systemDefault();
|
|
|
+
|
|
|
+ return instant.atZone(zoneId).toLocalDate();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 去年十二月第一天
|
|
|
+ *
|
|
|
+ * @param date 参照的日期
|
|
|
+ * @return 去年十二月第一天
|
|
|
+ * @author lixing
|
|
|
+ * @version V1.0 2021/5/30 11:43 上午
|
|
|
+ */
|
|
|
+ public static Date getDecemberFirstDayOfLastYear(Date date) {
|
|
|
+ if (date == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ Instant instant = date.toInstant();
|
|
|
+ ZoneId zoneId = ZoneId.systemDefault();
|
|
|
+
|
|
|
+ LocalDate localDate = instant.atZone(zoneId).toLocalDate();
|
|
|
+ LocalDate lastYear = localDate.minusYears(1);
|
|
|
+ LocalDate lastYearFirstDay = LocalDate.of(lastYear.getYear(), 12, 1);
|
|
|
+ return Date.from(lastYearFirstDay.atStartOfDay(zoneId).toInstant());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 给定年份十二月第一天
|
|
|
+ *
|
|
|
+ * @param date 参照的日期
|
|
|
+ * @return 十二月第一天
|
|
|
+ * @author lixing
|
|
|
+ * @version V1.0 2021/5/30 11:43 上午
|
|
|
+ */
|
|
|
+ public static Date getDecemberFirstDayOfYear(Date date) {
|
|
|
+ if (date == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ Instant instant = date.toInstant();
|
|
|
+ ZoneId zoneId = ZoneId.systemDefault();
|
|
|
+
|
|
|
+ LocalDate localDate = instant.atZone(zoneId).toLocalDate();
|
|
|
+ LocalDate decemberFirstDayOfYear = LocalDate.of(localDate.getYear(), 12, 1);
|
|
|
+ return Date.from(decemberFirstDayOfYear.atStartOfDay(zoneId).toInstant());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 去年同期月份第一天
|
|
|
+ *
|
|
|
+ * @param date 参照的日期
|
|
|
+ * @return 去年同期月份第一天
|
|
|
+ * @author lixing
|
|
|
+ * @version V1.0 2021/5/30 11:43 上午
|
|
|
+ */
|
|
|
+ public static Date getSameMonthFirstDayOfLastYear(Date date) {
|
|
|
+ if (date == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ Instant instant = date.toInstant();
|
|
|
+ ZoneId zoneId = ZoneId.systemDefault();
|
|
|
+
|
|
|
+ LocalDate localDate = instant.atZone(zoneId).toLocalDate();
|
|
|
+ LocalDate lastYear = localDate.minusYears(1);
|
|
|
+ LocalDate lastYearFirstDay = LocalDate.of(lastYear.getYear(), lastYear.getMonth(), 1);
|
|
|
+ return Date.from(lastYearFirstDay.atStartOfDay(zoneId).toInstant());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ * 获取阶段内的每个月的第一天
|
|
|
+ *
|
|
|
+ * @param startDate 开始日期
|
|
|
+ * @param endDate 结束日期
|
|
|
+ * @return 阶段内的每个月的第一天
|
|
|
+ * @author lixing
|
|
|
+ * @version V1.0 2021/5/30 11:43 上午
|
|
|
+ */
|
|
|
+ public static List<Date> getFirstDayOfEveryMonth(Date startDate, Date endDate) {
|
|
|
+ if (startDate == null || endDate == null) {
|
|
|
+ return Lists.newArrayList();
|
|
|
+ }
|
|
|
+
|
|
|
+ ZoneId zoneId = ZoneId.systemDefault();
|
|
|
+ LocalDate startLocalDate = startDate.toInstant().atZone(zoneId).toLocalDate();
|
|
|
+ LocalDate endLocalDate = endDate.toInstant().atZone(zoneId).toLocalDate();
|
|
|
+
|
|
|
+ int startYear = startLocalDate.getYear();
|
|
|
+ int startYearMonth = startLocalDate.getMonthValue();
|
|
|
+ int endYear = endLocalDate.getYear();
|
|
|
+ int endYearMonth = endLocalDate.getMonthValue();
|
|
|
+ boolean startYearBigThanEndYear = startYear > endYear;
|
|
|
+
|
|
|
+ boolean startMonthBigThanEndMonth = startYear == endYear && startYearMonth > endYearMonth;
|
|
|
+
|
|
|
+ if (startYearBigThanEndYear || startMonthBigThanEndMonth) {
|
|
|
+ return Lists.newArrayList();
|
|
|
+ }
|
|
|
+
|
|
|
+ List<Date> result = new ArrayList<>();
|
|
|
+ for (int i = startYear; i <= endYear; i++) {
|
|
|
+ int startMonth = 1;
|
|
|
+ int endMonth = 12;
|
|
|
+ if (i == startYear) {
|
|
|
+ startMonth = startYearMonth;
|
|
|
+ }
|
|
|
+ if (i == endYear) {
|
|
|
+ endMonth = endYearMonth;
|
|
|
+ }
|
|
|
+
|
|
|
+ for (int j = startMonth; j <= endMonth; j++) {
|
|
|
+ LocalDate tmp = LocalDate.of(i, j, 1);
|
|
|
+ result.add(Date.from(tmp.atStartOfDay(zoneId).toInstant()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 给定月份的第一天
|
|
|
+ *
|
|
|
+ * @param date 给定的日期
|
|
|
+ * @return 给定月份的第一天
|
|
|
+ * @author lixing
|
|
|
+ * @version V1.0 2021/5/30 11:44 上午
|
|
|
+ */
|
|
|
+ public static Date getFirstDayOfMonth(Date date) {
|
|
|
+ if (date == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ Instant instant = date.toInstant();
|
|
|
+ ZoneId zoneId = ZoneId.systemDefault();
|
|
|
+
|
|
|
+ LocalDate localDate = instant.atZone(zoneId).toLocalDate();
|
|
|
+ LocalDate firstDayOfMonth = LocalDate.of(localDate.getYear(), localDate.getMonth(), 1);
|
|
|
+ return Date.from(firstDayOfMonth.atStartOfDay(zoneId).toInstant());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 上个月的第一天
|
|
|
+ *
|
|
|
+ * @param date 参照的日期
|
|
|
+ * @return 上个月的第一天
|
|
|
+ * @author lixing
|
|
|
+ * @version V1.0 2021/5/30 11:44 上午
|
|
|
+ */
|
|
|
+ public static Date getFirstDayOfLastMonth(Date date) {
|
|
|
+ if (date == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ Instant instant = date.toInstant();
|
|
|
+ ZoneId zoneId = ZoneId.systemDefault();
|
|
|
+
|
|
|
+ LocalDate localDate = instant.atZone(zoneId).toLocalDate();
|
|
|
+ LocalDate lastMonth = localDate.minusMonths(1);
|
|
|
+ LocalDate lastMonthFirstDay = LocalDate.of(lastMonth.getYear(), lastMonth.getMonth(), 1);
|
|
|
+ return Date.from(lastMonthFirstDay.atStartOfDay(zoneId).toInstant());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 判断日期1是否早于日期2
|
|
|
+ *
|
|
|
+ * @param date1 日期1
|
|
|
+ * @param date2 日期2
|
|
|
+ * @return 判断结果
|
|
|
+ * @author lixing
|
|
|
+ * @version V1.0 2021/5/31 10:44 上午
|
|
|
+ */
|
|
|
+ public static boolean compareDate(Date date1, Date date2) {
|
|
|
+ if (date1 == null) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ if (date2 == null) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ ZoneId zoneId = ZoneId.systemDefault();
|
|
|
+
|
|
|
+ LocalDate localDate1 = date1.toInstant().atZone(zoneId).toLocalDate();
|
|
|
+ LocalDate localDate2 = date2.toInstant().atZone(zoneId).toLocalDate();
|
|
|
+ return localDate1.isBefore(localDate2);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ * 打印当前时间距离开始时间的毫秒数
|
|
|
+ *
|
|
|
+ * @param startDate 开始时间
|
|
|
+ * @author lixing
|
|
|
+ * @version V1.0 2021/6/23 7:01 下午
|
|
|
+ */
|
|
|
+ public static void printDuration(Date startDate) {
|
|
|
+ Date now = new Date();
|
|
|
+ System.out.println("用时:" + (now.getTime() - startDate.getTime()));
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 获取月份内的每一天
|
|
|
+ *
|
|
|
+ * @param month 给定月份
|
|
|
+ * @param pattern 返回值日期格式
|
|
|
+ * @return 月份内的每一天
|
|
|
+ * @author lixing
|
|
|
+ * @version V1.0 2021/9/2 10:28 上午
|
|
|
+ */
|
|
|
+ public static List<String> getDayListOfMonth(Date month, String pattern) {
|
|
|
+ if (month == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ List<String> list = new ArrayList<>();
|
|
|
+ Calendar aCalendar = Calendar.getInstance(Locale.CHINA);
|
|
|
+ Date firstDayOfMonth = getFirstDayOfMonth(month);
|
|
|
+ aCalendar.setTime(firstDayOfMonth);
|
|
|
+ int day = aCalendar.getActualMaximum(Calendar.DATE);
|
|
|
+
|
|
|
+ for (int i = 1; i <= day; i++) {
|
|
|
+ aCalendar.setTime(getDayOff(firstDayOfMonth, i-1));
|
|
|
+ String dateStr = date2Str(aCalendar.getTime(), pattern);
|
|
|
+ list.add(dateStr);
|
|
|
+ }
|
|
|
+
|
|
|
+ return list;
|
|
|
+
|
|
|
+ }
|
|
|
+}
|