|
@@ -0,0 +1,955 @@
|
|
|
+package com.ruoyi.common.utils.time;
|
|
|
+
|
|
|
+import cn.hutool.core.comparator.CompareUtil;
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
+import com.google.common.collect.Lists;
|
|
|
+import com.ruoyi.common.enums.Granularity;
|
|
|
+import com.ruoyi.common.enums.TimeHorizon;
|
|
|
+import com.ruoyi.common.utils.DateUtils;
|
|
|
+import lombok.SneakyThrows;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.joda.time.DateTime;
|
|
|
+
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+public class TimeUtils {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 时间格式(yyyyMMdd)
|
|
|
+ */
|
|
|
+ public final static String YEARMONTHDAY_PATTERN = "yyyyMMdd";
|
|
|
+ /**
|
|
|
+ * 时间格式(yyyy)
|
|
|
+ */
|
|
|
+ public final static String YEAR_PATTERN = "yyyy";
|
|
|
+ /**
|
|
|
+ * 时间格式(yyyyMM)
|
|
|
+ */
|
|
|
+ public final static String MONTHS_PATTERN = "yyyyMM";
|
|
|
+ /**
|
|
|
+ * 时间格式(yyyy-MM)
|
|
|
+ */
|
|
|
+ public final static String MONTH_PATTERN = "yyyy-MM";
|
|
|
+ /**
|
|
|
+ * 时间格式(yyyy-MM-dd)
|
|
|
+ */
|
|
|
+ public final static String DATE_PATTERN = "yyyy-MM-dd";
|
|
|
+ /**
|
|
|
+ * 时间格式(yyyy-MM-dd HH)
|
|
|
+ */
|
|
|
+ public static final String DATE_HOUR_PATTERN = "yyyy-MM-dd HH";
|
|
|
+ /**
|
|
|
+ * 时间格式(yyyy-MM-dd HH:mm:ss)
|
|
|
+ */
|
|
|
+ public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
|
|
|
+ /**
|
|
|
+ * 时间格式(yyyy-MM-dd HH:mm:ss)
|
|
|
+ */
|
|
|
+ public final static String DATE_TIME_PATTERN_MSG = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 日期解析
|
|
|
+ *
|
|
|
+ * @param date 日期
|
|
|
+ * @param pattern 格式,如:DateUtils.DATE_TIME_PATTERN
|
|
|
+ * @return 返回Date
|
|
|
+ */
|
|
|
+ @SneakyThrows
|
|
|
+ public static Date parse(String date, String pattern) {
|
|
|
+ return new SimpleDateFormat(pattern).parse(date);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 日期格式化 日期格式为:yyyy-MM-dd
|
|
|
+ *
|
|
|
+ * @param date 日期
|
|
|
+ * @param pattern 格式,如:DateUtils.DATE_TIME_PATTERN
|
|
|
+ * @return 返回yyyy-MM-dd格式日期
|
|
|
+ */
|
|
|
+ public static String format(Date date, String pattern) {
|
|
|
+ if (ObjectUtil.isNull(date)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ SimpleDateFormat df = new SimpleDateFormat(pattern);
|
|
|
+ return df.format(date);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取起始时间之间包含的小时,返回一个小时的开始时间,以yyyy-MM-dd HH:mm:ss格式返回
|
|
|
+ *
|
|
|
+ * @param startTm
|
|
|
+ * @param endTm
|
|
|
+ * @return ["2021-10-01 01:00:00","2021-10-01 02:00:00","2021-10-01 03:00:00"]
|
|
|
+ */
|
|
|
+ public static List<String> getHourListBetween(String startTm, String endTm) {
|
|
|
+ Date beginDate = parse(startTm, DATE_TIME_PATTERN);
|
|
|
+ Date endDate = parse(endTm, DATE_TIME_PATTERN);
|
|
|
+ //重置成了开始时间的月初时间
|
|
|
+ Date resetDate = parse(format(parse(startTm, DATE_HOUR_PATTERN), DATE_TIME_PATTERN), DATE_TIME_PATTERN);
|
|
|
+ resetDate = beginDate.compareTo(resetDate) > 0 ? DateUtils.addHours(resetDate, 1) : resetDate;
|
|
|
+
|
|
|
+ List<String> hourList = Lists.newArrayList();
|
|
|
+ while (resetDate.compareTo(endDate) <= 0) {
|
|
|
+ hourList.add(format(resetDate, DATE_TIME_PATTERN));
|
|
|
+ resetDate = DateUtils.addHours(resetDate, 1);
|
|
|
+ }
|
|
|
+
|
|
|
+ return hourList;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取起始时间之间包含的日期,返回一天的开始时间,以yyyy-MM-dd HH:mm:ss格式返回
|
|
|
+ *
|
|
|
+ * @param startTm
|
|
|
+ * @param endTm
|
|
|
+ * @return ["2021-10-01 00:00:00","2021-10-02 00:00:00","2021-10-03 00:00:00"]
|
|
|
+ */
|
|
|
+ @SneakyThrows
|
|
|
+ public static List<String> getDateListBetween(String startTm, String endTm) {
|
|
|
+ Date beginDate = parse(startTm, DATE_TIME_PATTERN);
|
|
|
+ Date endDate = parse(endTm, DATE_TIME_PATTERN);
|
|
|
+ //重置成了开始时间的每天开始时间
|
|
|
+ Date resetDate = parse(format(parse(startTm, DATE_PATTERN), DATE_TIME_PATTERN), DATE_TIME_PATTERN);
|
|
|
+ resetDate = beginDate.compareTo(resetDate) > 0 ? DateUtils.addDays(resetDate, 1) : resetDate;
|
|
|
+
|
|
|
+ List<String> dateList = Lists.newArrayList();
|
|
|
+ while (resetDate.compareTo(endDate) <= 0) {
|
|
|
+ dateList.add(format(resetDate, DATE_TIME_PATTERN));
|
|
|
+ resetDate = DateUtils.addDays(resetDate, 1);
|
|
|
+ }
|
|
|
+
|
|
|
+ return dateList;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取起始时间之间包含的月份,返回月初的时间,以yyyy-MM-dd HH:mm:ss格式返回
|
|
|
+ *
|
|
|
+ * @param startTm
|
|
|
+ * @param endTm
|
|
|
+ * @return ["2021-10-01 00:00:00","2021-11-01 00:00:00","2021-12-01 00:00:00"]
|
|
|
+ */
|
|
|
+ @SneakyThrows
|
|
|
+ public static List<String> getMonthListBetween(String startTm, String endTm) {
|
|
|
+ Date beginDate = parse(startTm, DATE_TIME_PATTERN);
|
|
|
+ Date endDate = parse(endTm, DATE_TIME_PATTERN);
|
|
|
+ //重置成了开始时间的月初时间
|
|
|
+ Date resetDate = parse(format(parse(startTm, MONTH_PATTERN), DATE_TIME_PATTERN), DATE_TIME_PATTERN);
|
|
|
+ resetDate = beginDate.compareTo(resetDate) > 0 ? DateUtils.addMonths(resetDate, 1) : resetDate;
|
|
|
+
|
|
|
+ List<String> monthList = Lists.newArrayList();
|
|
|
+ while (resetDate.compareTo(endDate) <= 0) {
|
|
|
+ monthList.add(format(resetDate, DATE_TIME_PATTERN));
|
|
|
+ resetDate = DateUtils.addMonths(resetDate, 1);
|
|
|
+ }
|
|
|
+ return monthList;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取起始时间之间包含的年份,返回年初的时间,以yyyy-MM-dd HH:mm:ss格式返回
|
|
|
+ *
|
|
|
+ * @param startTm 2019-09-28 16:34:00
|
|
|
+ * @param endTm 2021-12-19 10:36:11
|
|
|
+ * @return ["2020-01-01 00:00:00","2021-01-01 00:00:00"]
|
|
|
+ */
|
|
|
+ @SneakyThrows
|
|
|
+ public static List<String> getYearListBetween(String startTm, String endTm) {
|
|
|
+ Date beginDate = parse(startTm, DATE_TIME_PATTERN);
|
|
|
+ Date endDate = parse(endTm, DATE_TIME_PATTERN);
|
|
|
+ //重置成了开始时间的月初时间
|
|
|
+ Date resetDate = parse(format(parse(startTm, YEAR_PATTERN), DATE_TIME_PATTERN), DATE_TIME_PATTERN);
|
|
|
+ resetDate = beginDate.compareTo(resetDate) > 0 ? DateUtils.addDays(resetDate, 1) : resetDate;
|
|
|
+
|
|
|
+ List<String> yearList = Lists.newArrayList();
|
|
|
+ while (resetDate.compareTo(endDate) <= 0) {
|
|
|
+ yearList.add(format(resetDate, DATE_TIME_PATTERN));
|
|
|
+ resetDate = DateUtils.addYears(resetDate, 1);
|
|
|
+ }
|
|
|
+
|
|
|
+ return yearList;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 切割时间,返回时间列表
|
|
|
+ *
|
|
|
+ * @param startTime 开始时间
|
|
|
+ * @param endTime 结束时间
|
|
|
+ * @param granularity 切割时间的粒度 小时-Granularity.HOUR 天-Granularity.DAY 月-Granularity.MONTH
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static List<SplitedTimeParam> getSplitedTimeList(Long startTime, Long endTime, Granularity granularity) {
|
|
|
+ Map<String, SplitedTimeParam> splitedTimeGroupByTime = getSplitedTimeGroupByTime(timestampToDate(startTime), timestampToDate(endTime), granularity);
|
|
|
+ return splitedTimeGroupByTime.entrySet().stream().map(item -> item.getValue()).collect(Collectors.toList());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 切割时间
|
|
|
+ *
|
|
|
+ * @param startTime 开始时间
|
|
|
+ * @param endTime 结束时间
|
|
|
+ * @param granularity 切割时间的粒度 小时-Granularity.HOUR 天-Granularity.DAY 月-Granularity.MONTH
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Map<String, SplitedTimeParam> getSplitedTimeGroupByTime(Long startTime, Long endTime, Granularity granularity) {
|
|
|
+ return getSplitedTimeGroupByTime(timestampToDate(startTime), timestampToDate(endTime), granularity);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 切割时间
|
|
|
+ *
|
|
|
+ * @param startTm 开始时间 "yyyy-MM-dd HH:mm:ss"
|
|
|
+ * @param endTm 结束时间 "yyyy-MM-dd HH:mm:ss"
|
|
|
+ * @param granularity 切割时间的粒度 小时-Granularity.HOUR 天-Granularity.DAY 月-Granularity.MONTH
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Map<String, SplitedTimeParam> getSplitedTimeGroupByTime(String startTm, String endTm, Granularity granularity) {
|
|
|
+ Date beginDate = parse(startTm, DATE_TIME_PATTERN);
|
|
|
+ Date endDate = parse(endTm, DATE_TIME_PATTERN);
|
|
|
+ return getSplitedTimeGroupByTime(beginDate, endDate, granularity);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 切割时间 20211109-仅支持小时日月的切割
|
|
|
+ *
|
|
|
+ * @param beginDate 开始时间
|
|
|
+ * @param endDate 结束时间
|
|
|
+ * @param granularity 切割时间的粒度 小时-Granularity.HOUR 天-Granularity.DAY 月-Granularity.MONTH
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Map<String, SplitedTimeParam> getSplitedTimeGroupByTime(Date beginDate, Date endDate, Granularity granularity) {
|
|
|
+ switch (granularity) {
|
|
|
+ case HOUR:
|
|
|
+ return getSplitedTimeMapByHour(beginDate, endDate);
|
|
|
+ case DAY:
|
|
|
+ return getSplitedTimeMapByDay(beginDate, endDate);
|
|
|
+ case MONTH:
|
|
|
+ return getSplitedTimeMapByMonth(beginDate, endDate);
|
|
|
+ case YEAR:
|
|
|
+ return getSplitedTimeMapByYear(beginDate, endDate);
|
|
|
+ default:
|
|
|
+ return getSplitedTimeMapByDay(beginDate, endDate);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 按小时切割时间,返回一个列表
|
|
|
+ *
|
|
|
+ * @param startTm 开始时间 "yyyy-MM-dd HH:mm:ss"
|
|
|
+ * @param endTm 结束时间 "yyyy-MM-dd HH:mm:ss"
|
|
|
+ * @return [{"beginTime":"2021-11-03 22:28:59","endTime":"2021-11-03 22:59:59"},{"beginTime":"2021-11-03 23:00:00","endTime":"2021-11-03 23:56:50"}]
|
|
|
+ */
|
|
|
+ public static List<SplitedTimeParam> getSplitedTimeListByHour(String startTm, String endTm) {
|
|
|
+ Date beginDate = parse(startTm, DATE_TIME_PATTERN);
|
|
|
+ Date endDate = parse(endTm, DATE_TIME_PATTERN);
|
|
|
+ Map<String, SplitedTimeParam> splitedTimeParamMap = getSplitedTimeMapByHour(beginDate, endDate);
|
|
|
+ return Lists.newArrayList(splitedTimeParamMap.values());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 按小时切割时间,返回一个列表
|
|
|
+ *
|
|
|
+ * @param beginDate 开始时间
|
|
|
+ * @param endDate 结束时间
|
|
|
+ * @return [{"beginTime":"2021-11-03 22:28:59","endTime":"2021-11-03 22:59:59"},{"beginTime":"2021-11-03 23:00:00","endTime":"2021-11-03 23:56:50"}]
|
|
|
+ */
|
|
|
+ public static List<SplitedTimeParam> getSplitedTimeListByHour(Date beginDate, Date endDate) {
|
|
|
+ Map<String, SplitedTimeParam> splitedTimeParamMap = getSplitedTimeMapByHour(beginDate, endDate);
|
|
|
+ return Lists.newArrayList(splitedTimeParamMap.values());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 按小时切割时间,返回一个映射
|
|
|
+ *
|
|
|
+ * @param startTm 开始时间 "yyyy-MM-dd HH:mm:ss"
|
|
|
+ * @param endTm 结束时间 "yyyy-MM-dd HH:mm:ss"
|
|
|
+ * @return {"2021-11-03 22:00:00":{"beginTime":"2021-11-03 22:28:59","endTime":"2021-11-03 22:59:59"},"2021-11-03 23:00:00":{"beginTime":"2021-11-03 23:00:00","endTime":"2021-11-03 23:56:50"}}
|
|
|
+ */
|
|
|
+ public static Map<String, SplitedTimeParam> getSplitedTimeMapByHour(String startTm, String endTm) {
|
|
|
+ Date beginDate = parse(startTm, DATE_TIME_PATTERN);
|
|
|
+ Date endDate = parse(endTm, DATE_TIME_PATTERN);
|
|
|
+ return getSplitedTimeMapByHour(beginDate, endDate);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 按小时切割时间,返回一个映射
|
|
|
+ *
|
|
|
+ * @param beginDate 开始时间
|
|
|
+ * @param endDate 结束时间
|
|
|
+ * @return {"2021-11-03 22:00:00":{"beginTime":"2021-11-03 22:28:59","endTime":"2021-11-03 22:59:59"},"2021-11-03 23:00:00":{"beginTime":"2021-11-03 23:00:00","endTime":"2021-11-03 23:56:50"}}
|
|
|
+ */
|
|
|
+ public static Map<String, SplitedTimeParam> getSplitedTimeMapByHour(Date beginDate, Date endDate) {
|
|
|
+ String startTm = format(beginDate, DATE_TIME_PATTERN);
|
|
|
+ String endTm = format(endDate, DATE_TIME_PATTERN);
|
|
|
+
|
|
|
+ Map<String, SplitedTimeParam> splitedTimeMap = new LinkedHashMap<>();
|
|
|
+ if (CompareUtil.compare(endDate, beginDate) < 0) {
|
|
|
+ log.warn("endDate is earlier than beginDate, beginDate:{}, endDate:{}", startTm, endTm);
|
|
|
+ return splitedTimeMap;
|
|
|
+ }
|
|
|
+ Date resetDate = parse(format(parse(startTm, DATE_HOUR_PATTERN), DATE_TIME_PATTERN), DATE_TIME_PATTERN);
|
|
|
+ while (CompareUtil.compare(resetDate, endDate) <= 0) {
|
|
|
+ SplitedTimeParam splitedTimeParam = new SplitedTimeParam();
|
|
|
+ if (CompareUtil.compare(resetDate, beginDate) <= 0) {
|
|
|
+ splitedTimeParam.setBeginTime(startTm);
|
|
|
+ } else if (CompareUtil.compare(resetDate, beginDate) > 0) {
|
|
|
+ splitedTimeParam.setBeginTime(format(resetDate, DATE_TIME_PATTERN));
|
|
|
+ }
|
|
|
+ if (CompareUtil.compare(DateUtils.addHours(resetDate, 1), endDate) > 0) {
|
|
|
+ splitedTimeParam.setEndTime(endTm);
|
|
|
+ } else if (CompareUtil.compare(DateUtils.addHours(resetDate, 1), endDate) <= 0) {
|
|
|
+ Date timePeriodEndDate = DateUtils.addSeconds(DateUtils.addHours(resetDate, 1), -1);
|
|
|
+ splitedTimeParam.setEndTime(format(timePeriodEndDate, DATE_TIME_PATTERN));
|
|
|
+ }
|
|
|
+ splitedTimeMap.put(format(resetDate, DATE_TIME_PATTERN), splitedTimeParam);
|
|
|
+ resetDate = DateUtils.addHours(resetDate, 1);
|
|
|
+ }
|
|
|
+ return splitedTimeMap;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 按天切割时间,返回一个列表
|
|
|
+ *
|
|
|
+ * @param startTm 开始时间 "yyyy-MM-dd HH:mm:ss"
|
|
|
+ * @param endTm 结束时间 "yyyy-MM-dd HH:mm:ss"
|
|
|
+ * @return [{"beginTime":"2021-11-02 22:28:59","endTime":"2021-11-02 23:59:59"},{"beginTime":"2021-11-03 00:00:00","endTime":"2021-11-03 23:56:50"}]
|
|
|
+ */
|
|
|
+ public static List<SplitedTimeParam> getSplitedTimeListByDay(String startTm, String endTm) {
|
|
|
+ Date beginDate = parse(startTm, DATE_TIME_PATTERN);
|
|
|
+ Date endDate = parse(endTm, DATE_TIME_PATTERN);
|
|
|
+ Map<String, SplitedTimeParam> splitedTimeParamMap = getSplitedTimeMapByDay(beginDate, endDate);
|
|
|
+ return Lists.newArrayList(splitedTimeParamMap.values());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 按天切割时间,返回一个列表
|
|
|
+ *
|
|
|
+ * @param beginDate 开始时间
|
|
|
+ * @param endDate 结束时间
|
|
|
+ * @return [{"beginTime":"2021-11-02 22:28:59","endTime":"2021-11-02 23:59:59"},{"beginTime":"2021-11-03 00:00:00","endTime":"2021-11-03 23:56:50"}]
|
|
|
+ */
|
|
|
+ public static List<SplitedTimeParam> getSplitedTimeListByDay(Date beginDate, Date endDate) {
|
|
|
+ Map<String, SplitedTimeParam> splitedTimeParamMap = getSplitedTimeMapByDay(beginDate, endDate);
|
|
|
+ return Lists.newArrayList(splitedTimeParamMap.values());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 按天分割时间,返回一个映射
|
|
|
+ *
|
|
|
+ * @param startTm 开始时间 "yyyy-MM-dd HH:mm:ss"
|
|
|
+ * @param endTm 结束时间 "yyyy-MM-dd HH:mm:ss"
|
|
|
+ * @return {"2021-11-02 00:00:00":{"beginTime":"2021-11-02 22:28:59","endTime":"2021-11-02 23:59:59"},"2021-11-03 00:00:00":{"beginTime":"2021-11-03 00:00:00","endTime":"2021-11-03 23:56:50"}}
|
|
|
+ */
|
|
|
+ public static Map<String, SplitedTimeParam> getSplitedTimeMapByDay(String startTm, String endTm) {
|
|
|
+ Date beginDate = parse(startTm, DATE_TIME_PATTERN);
|
|
|
+ Date endDate = parse(endTm, DATE_TIME_PATTERN);
|
|
|
+ return getSplitedTimeMapByDay(beginDate, endDate);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 按天分割时间,返回一个映射
|
|
|
+ *
|
|
|
+ * @param beginDate 开始时间
|
|
|
+ * @param endDate 结束时间
|
|
|
+ * @return {"2021-11-02 00:00:00":{"beginTime":"2021-11-02 22:28:59","endTime":"2021-11-02 23:59:59"},"2021-11-03 00:00:00":{"beginTime":"2021-11-03 00:00:00","endTime":"2021-11-03 23:56:50"}}
|
|
|
+ */
|
|
|
+ public static Map<String, SplitedTimeParam> getSplitedTimeMapByDay(Date beginDate, Date endDate) {
|
|
|
+ String startTm = format(beginDate, DATE_TIME_PATTERN);
|
|
|
+ String endTm = format(endDate, DATE_TIME_PATTERN);
|
|
|
+
|
|
|
+ Map<String, SplitedTimeParam> splitedTimeMap = new LinkedHashMap<>();
|
|
|
+ if (CompareUtil.compare(endDate, beginDate) < 0) {
|
|
|
+ log.warn("endDate is earlier than beginDate, beginDate:{}, endDate:{}", startTm, endTm);
|
|
|
+ return splitedTimeMap;
|
|
|
+ }
|
|
|
+ Date resetDate = parse(format(parse(startTm, DATE_PATTERN), DATE_TIME_PATTERN), DATE_TIME_PATTERN);
|
|
|
+ while (CompareUtil.compare(resetDate, endDate) <= 0) {
|
|
|
+ SplitedTimeParam splitedTimeParam = new SplitedTimeParam();
|
|
|
+ if (CompareUtil.compare(resetDate, beginDate) <= 0) {
|
|
|
+ splitedTimeParam.setBeginTime(startTm);
|
|
|
+ } else if (CompareUtil.compare(resetDate, beginDate) > 0) {
|
|
|
+ splitedTimeParam.setBeginTime(format(resetDate, DATE_TIME_PATTERN));
|
|
|
+ }
|
|
|
+
|
|
|
+ if (CompareUtil.compare(DateUtils.addDays(resetDate, 1), endDate) > 0) {
|
|
|
+ splitedTimeParam.setEndTime(endTm);
|
|
|
+ } else if (CompareUtil.compare(DateUtils.addDays(resetDate, 1), endDate) <= 0) {
|
|
|
+ Date timePeriodEndDate = DateUtils.addSeconds(DateUtils.addDays(resetDate, 1), -1);
|
|
|
+ splitedTimeParam.setEndTime(format(timePeriodEndDate, DATE_TIME_PATTERN));
|
|
|
+ }
|
|
|
+ splitedTimeMap.put(format(resetDate, DATE_TIME_PATTERN), splitedTimeParam);
|
|
|
+ resetDate = DateUtils.addDays(resetDate, 1);
|
|
|
+ }
|
|
|
+ return splitedTimeMap;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 按月切割时间,返回一个列表
|
|
|
+ *
|
|
|
+ * @param startTm 开始时间 "yyyy-MM-dd HH:mm:ss"
|
|
|
+ * @param endTm 结束时间 "yyyy-MM-dd HH:mm:ss"
|
|
|
+ * @return [{"beginTime":"2021-10-02 22:28:59","endTime":"2021-10-31 23:59:59"},{"beginTime":"2021-11-01 00:00:00","endTime":"2021-11-03 23:56:50"}]
|
|
|
+ */
|
|
|
+ public static List<SplitedTimeParam> getSplitedTimeListByMonth(String startTm, String endTm) {
|
|
|
+ Date beginDate = parse(startTm, DATE_TIME_PATTERN);
|
|
|
+ Date endDate = parse(endTm, DATE_TIME_PATTERN);
|
|
|
+ Map<String, SplitedTimeParam> splitedTimeParamMap = getSplitedTimeMapByMonth(beginDate, endDate);
|
|
|
+ return Lists.newArrayList(splitedTimeParamMap.values());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 按月切割时间,返回一个列表
|
|
|
+ *
|
|
|
+ * @param beginDate 开始时间
|
|
|
+ * @param endDate 结束时间
|
|
|
+ * @return [{"beginTime":"2021-10-02 22:28:59","endTime":"2021-10-31 23:59:59"},{"beginTime":"2021-11-01 00:00:00","endTime":"2021-11-03 23:56:50"}]
|
|
|
+ */
|
|
|
+ public static List<SplitedTimeParam> getSplitedTimeListByMonth(Date beginDate, Date endDate) {
|
|
|
+ Map<String, SplitedTimeParam> splitedTimeParamMap = getSplitedTimeMapByMonth(beginDate, endDate);
|
|
|
+ return Lists.newArrayList(splitedTimeParamMap.values());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 按月分割时间,返回一个映射
|
|
|
+ *
|
|
|
+ * @param startTm 开始时间 "yyyy-MM-dd HH:mm:ss"
|
|
|
+ * @param endTm 结束时间 "yyyy-MM-dd HH:mm:ss"
|
|
|
+ * @return {"2021-10-01 00:00:00":{"beginTime":"2021-10-02 22:28:59","endTime":"2021-10-31 23:59:59"},"2021-11-01 00:00:00":{"beginTime":"2021-11-01 00:00:00","endTime":"2021-11-03 23:56:50"}}
|
|
|
+ */
|
|
|
+ public static Map<String, SplitedTimeParam> getSplitedTimeMapByMonth(String startTm, String endTm) {
|
|
|
+ Date beginDate = parse(startTm, DATE_TIME_PATTERN);
|
|
|
+ Date endDate = parse(endTm, DATE_TIME_PATTERN);
|
|
|
+ return getSplitedTimeMapByMonth(beginDate, endDate);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ System.out.println(getSplitedTimeMapByMonth("2023-08-07 22:28:59", "2023-09-07 23:59:58"));
|
|
|
+ System.out.println(getSplitedTimeMapByDay("2023-08-03 22:28:59", "2023-08-07 23:59:58"));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 按月分割时间,返回一个映射
|
|
|
+ *
|
|
|
+ * @param beginDate 开始时间
|
|
|
+ * @param endDate 结束时间
|
|
|
+ * @return {"2021-10-01 00:00:00":{"beginTime":"2021-10-02 22:28:59","endTime":"2021-10-31 23:59:59"},"2021-11-01 00:00:00":{"beginTime":"2021-11-01 00:00:00","endTime":"2021-11-03 23:56:50"}}
|
|
|
+ */
|
|
|
+ public static Map<String, SplitedTimeParam> getSplitedTimeMapByMonth(Date beginDate, Date endDate) {
|
|
|
+ String startTm = format(beginDate, DATE_TIME_PATTERN);
|
|
|
+ String endTm = format(endDate, DATE_TIME_PATTERN);
|
|
|
+
|
|
|
+ Map<String, SplitedTimeParam> splitedTimeMap = new LinkedHashMap<>();
|
|
|
+ if (CompareUtil.compare(endDate, beginDate) < 0) {
|
|
|
+ log.warn("endDate is earlier than beginDate, beginDate:{}, endDate:{}", startTm, endTm);
|
|
|
+ return splitedTimeMap;
|
|
|
+ }
|
|
|
+ Date resetDate = parse(format(parse(startTm, MONTH_PATTERN), DATE_TIME_PATTERN), DATE_TIME_PATTERN);
|
|
|
+ while (CompareUtil.compare(resetDate, endDate) <= 0) {
|
|
|
+ SplitedTimeParam splitedTimeParam = new SplitedTimeParam();
|
|
|
+ if (CompareUtil.compare(resetDate, beginDate) <= 0) {
|
|
|
+ splitedTimeParam.setBeginTime(startTm);
|
|
|
+ } else if (CompareUtil.compare(resetDate, beginDate) > 0) {
|
|
|
+ splitedTimeParam.setBeginTime(format(resetDate, DATE_TIME_PATTERN));
|
|
|
+ }
|
|
|
+ if (CompareUtil.compare(DateUtils.addMonths(resetDate, 1), endDate) > 0) {
|
|
|
+ splitedTimeParam.setEndTime(endTm);
|
|
|
+ } else if (CompareUtil.compare(DateUtils.addMonths(resetDate, 1), endDate) <= 0) {
|
|
|
+ Date timePeriodEndDate = DateUtils.addSeconds(DateUtils.addMonths(resetDate, 1), -1);
|
|
|
+ splitedTimeParam.setEndTime(format(timePeriodEndDate, DATE_TIME_PATTERN));
|
|
|
+ }
|
|
|
+ splitedTimeMap.put(format(resetDate, DATE_TIME_PATTERN), splitedTimeParam);
|
|
|
+ resetDate = DateUtils.addMonths(resetDate, 1);
|
|
|
+ }
|
|
|
+ return splitedTimeMap;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 按年分割时间,返回一个映射
|
|
|
+ *
|
|
|
+ * @param startTm 开始时间
|
|
|
+ * @param endTm 结束时间
|
|
|
+ * @return {2022-01-01 00:00:00=SplitedTimeParam(beginTime=2022-04-03 00:00:00, endTime=2022-12-31 23:59:59), 2023-01-01 00:00:00=SplitedTimeParam(beginTime=2023-01-01 00:00:00, endTime=2023-04-03 00:00:00)}
|
|
|
+ */
|
|
|
+ public static Map<String, SplitedTimeParam> getSplitedTimeMapByYear(String startTm, String endTm) {
|
|
|
+ Date beginDate = parse(startTm, DATE_TIME_PATTERN);
|
|
|
+ Date endDate = parse(endTm, DATE_TIME_PATTERN);
|
|
|
+ return getSplitedTimeMapByYear(beginDate, endDate);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 按年分割时间,返回一个映射
|
|
|
+ *
|
|
|
+ * @param beginDate 开始时间
|
|
|
+ * @param endDate 结束时间
|
|
|
+ * @return {2022-01-01 00:00:00=SplitedTimeParam(beginTime=2022-04-03 00:00:00, endTime=2022-12-31 23:59:59), 2023-01-01 00:00:00=SplitedTimeParam(beginTime=2023-01-01 00:00:00, endTime=2023-04-03 00:00:00)}
|
|
|
+ */
|
|
|
+ public static Map<String, SplitedTimeParam> getSplitedTimeMapByYear(Date beginDate, Date endDate) {
|
|
|
+ String startTm = format(beginDate, DATE_TIME_PATTERN);
|
|
|
+ String endTm = format(endDate, DATE_TIME_PATTERN);
|
|
|
+
|
|
|
+ Map<String, SplitedTimeParam> splitedTimeMap = new LinkedHashMap<>();
|
|
|
+ if (CompareUtil.compare(endDate, beginDate) < 0) {
|
|
|
+ log.warn("endDate is earlier than beginDate, beginDate:{}, endDate:{}", startTm, endTm);
|
|
|
+ return splitedTimeMap;
|
|
|
+ }
|
|
|
+ Date resetDate = parse(format(parse(startTm, YEAR_PATTERN), DATE_TIME_PATTERN), DATE_TIME_PATTERN);
|
|
|
+ while (CompareUtil.compare(resetDate, endDate) <= 0) {
|
|
|
+ SplitedTimeParam splitedTimeParam = new SplitedTimeParam();
|
|
|
+ if (CompareUtil.compare(resetDate, beginDate) <= 0) {
|
|
|
+ splitedTimeParam.setBeginTime(startTm);
|
|
|
+ } else if (CompareUtil.compare(resetDate, beginDate) > 0) {
|
|
|
+ splitedTimeParam.setBeginTime(format(resetDate, DATE_TIME_PATTERN));
|
|
|
+ }
|
|
|
+ if (CompareUtil.compare(DateUtils.addYears(resetDate, 1), endDate) > 0) {
|
|
|
+ splitedTimeParam.setEndTime(endTm);
|
|
|
+ } else if (CompareUtil.compare(DateUtils.addYears(resetDate, 1), endDate) <= 0) {
|
|
|
+ Date timePeriodEndDate = DateUtils.addSeconds(DateUtils.addYears(resetDate, 1), -1);
|
|
|
+ splitedTimeParam.setEndTime(format(timePeriodEndDate, DATE_TIME_PATTERN));
|
|
|
+ }
|
|
|
+ splitedTimeMap.put(format(resetDate, DATE_TIME_PATTERN), splitedTimeParam);
|
|
|
+ resetDate = DateUtils.addYears(resetDate, 1);
|
|
|
+ }
|
|
|
+ return splitedTimeMap;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得当前时间的所在天的第一秒
|
|
|
+ *
|
|
|
+ * @return yyyy-MM-dd HH:mm:ss
|
|
|
+ */
|
|
|
+ public static String getDayBeginTime() {
|
|
|
+ return getDayBeginTime(new Date());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得当前时间的所在天的第一秒
|
|
|
+ *
|
|
|
+ * @param currentDate 当前时间
|
|
|
+ * @return yyyy-MM-dd HH:mm:ss
|
|
|
+ */
|
|
|
+ public static String getDayBeginTime(Date currentDate) {
|
|
|
+ String currentTime = format(currentDate, DATE_TIME_PATTERN);
|
|
|
+ return getDayBeginTime(currentTime);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得当前时间的所在天的起始时间
|
|
|
+ *
|
|
|
+ * @param currentTime 当前时间 yyyy-MM-dd HH:mm:ss
|
|
|
+ * @return yyyy-MM-dd HH:mm:ss
|
|
|
+ */
|
|
|
+ public static String getDayBeginTime(String currentTime) {
|
|
|
+ return format(parse(currentTime, DATE_PATTERN), DATE_TIME_PATTERN);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得当前时间的所在天的最后一秒
|
|
|
+ *
|
|
|
+ * @return yyyy-MM-dd HH:mm:ss
|
|
|
+ */
|
|
|
+ public static String getDayEndTime() {
|
|
|
+ String currentTime = format(new Date(), DATE_TIME_PATTERN);
|
|
|
+ return getDayEndTime(currentTime);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得当前时间的所在天的最后一秒
|
|
|
+ *
|
|
|
+ * @param currentDate 当前时间
|
|
|
+ * @return yyyy-MM-dd HH:mm:ss
|
|
|
+ */
|
|
|
+ public static String getDayEndTime(Date currentDate) {
|
|
|
+ String currentTime = format(currentDate, DATE_TIME_PATTERN);
|
|
|
+ return getDayEndTime(currentTime);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得当前时间的所在天的最后一秒
|
|
|
+ *
|
|
|
+ * @param currentTime 当前时间 yyyy-MM-dd HH:mm:ss
|
|
|
+ * @return yyyy-MM-dd HH:mm:ss
|
|
|
+ */
|
|
|
+ public static String getDayEndTime(String currentTime) {
|
|
|
+ Date resetDate = parse(getDayBeginTime(currentTime), DATE_TIME_PATTERN);
|
|
|
+ return format(DateUtils.addSeconds(DateUtils.addDays(resetDate, 1), -1), DATE_TIME_PATTERN);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得当前时间的所在月的第一秒
|
|
|
+ *
|
|
|
+ * @return yyyy-MM-dd HH:mm:ss
|
|
|
+ */
|
|
|
+ public static String getMonthBeginTime() {
|
|
|
+ String currentTime = format(new Date(), DATE_TIME_PATTERN);
|
|
|
+ return getMonthBeginTime(currentTime);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得当前时间的所在月的第一秒
|
|
|
+ *
|
|
|
+ * @param currentDate 当前时间
|
|
|
+ * @return yyyy-MM-dd HH:mm:ss
|
|
|
+ */
|
|
|
+ public static String getMonthBeginTime(Date currentDate) {
|
|
|
+ String currentTime = format(currentDate, DATE_TIME_PATTERN);
|
|
|
+ return getMonthBeginTime(currentTime);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得当前时间的所在月的第一秒
|
|
|
+ *
|
|
|
+ * @param currentTime 当前时间 yyyy-MM-dd HH:mm:ss
|
|
|
+ * @return yyyy-MM-dd HH:mm:ss
|
|
|
+ */
|
|
|
+ public static String getMonthBeginTime(String currentTime) {
|
|
|
+ return format(parse(currentTime, MONTH_PATTERN), DATE_TIME_PATTERN);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得当前时间的所在月的最后一秒
|
|
|
+ *
|
|
|
+ * @return yyyy-MM-dd HH:mm:ss
|
|
|
+ */
|
|
|
+ public static String getMonthEndTime() {
|
|
|
+ String currentTime = format(new Date(), DATE_TIME_PATTERN);
|
|
|
+ return getMonthEndTime(currentTime);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得当前时间的所在月的最后一秒
|
|
|
+ *
|
|
|
+ * @param currentDate 当前时间
|
|
|
+ * @return yyyy-MM-dd HH:mm:ss
|
|
|
+ */
|
|
|
+ public static String getMonthEndTime(Date currentDate) {
|
|
|
+ String currentTime = format(currentDate, DATE_TIME_PATTERN);
|
|
|
+ return getMonthEndTime(currentTime);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得当前时间的所在月的最后一秒
|
|
|
+ *
|
|
|
+ * @param currentTime 当前时间 yyyy-MM-dd HH:mm:ss
|
|
|
+ * @return yyyy-MM-dd HH:mm:ss
|
|
|
+ */
|
|
|
+ public static String getMonthEndTime(String currentTime) {
|
|
|
+ Date resetDate = parse(getMonthBeginTime(currentTime), DATE_TIME_PATTERN);
|
|
|
+ return format(DateUtils.addSeconds(DateUtils.addMonths(resetDate, 1), -1), DATE_TIME_PATTERN);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得当前时间的所在年的第一秒
|
|
|
+ *
|
|
|
+ * @return yyyy-MM-dd HH:mm:ss
|
|
|
+ */
|
|
|
+ public static String getYearBeginTime() {
|
|
|
+ String currentTime = format(new Date(), DATE_TIME_PATTERN);
|
|
|
+ return getYearBeginTime(currentTime);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得当前时间的所在年的第一秒
|
|
|
+ *
|
|
|
+ * @param currentDate 当前时间
|
|
|
+ * @return yyyy-MM-dd HH:mm:ss
|
|
|
+ */
|
|
|
+ public static String getYearBeginTime(Date currentDate) {
|
|
|
+ String currentTime = format(currentDate, DATE_TIME_PATTERN);
|
|
|
+ return getYearBeginTime(currentTime);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得当前时间的所在年的第一秒
|
|
|
+ *
|
|
|
+ * @param currentTime 当前时间 yyyy-MM-dd HH:mm:ss
|
|
|
+ * @return yyyy-MM-dd HH:mm:ss
|
|
|
+ */
|
|
|
+ public static String getYearBeginTime(String currentTime) {
|
|
|
+ return format(parse(currentTime, YEAR_PATTERN), DATE_TIME_PATTERN);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得当前时间的所在年的最后一秒
|
|
|
+ *
|
|
|
+ * @return yyyy-MM-dd HH:mm:ss
|
|
|
+ */
|
|
|
+ public static String getYearEndTime() {
|
|
|
+ String currentTime = format(new Date(), DATE_TIME_PATTERN);
|
|
|
+ return getYearEndTime(currentTime);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得当前时间的所在年的最后一秒
|
|
|
+ *
|
|
|
+ * @param currentDate 当前时间
|
|
|
+ * @return yyyy-MM-dd HH:mm:ss
|
|
|
+ */
|
|
|
+ public static String getYearEndTime(Date currentDate) {
|
|
|
+ String currentTime = format(currentDate, DATE_TIME_PATTERN);
|
|
|
+ return getYearEndTime(currentTime);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得当前时间的所在年的最后一秒
|
|
|
+ *
|
|
|
+ * @param currentTime 当前时间 yyyy-MM-dd HH:mm:ss
|
|
|
+ * @return yyyy-MM-dd HH:mm:ss
|
|
|
+ */
|
|
|
+ public static String getYearEndTime(String currentTime) {
|
|
|
+ Date resetDate = parse(getYearBeginTime(currentTime), DATE_TIME_PATTERN);
|
|
|
+ return format(DateUtils.addSeconds(DateUtils.addYears(resetDate, 1), -1), DATE_TIME_PATTERN);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 字符串时间转时间戳
|
|
|
+ *
|
|
|
+ * @param currentTime 当前时间
|
|
|
+ * @param pattern 日期格式
|
|
|
+ * @return 返回时间戳
|
|
|
+ */
|
|
|
+ public static Long dateToTimestamp(String currentTime, String pattern) {
|
|
|
+ return parse(currentTime, pattern).getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 时间转时间戳
|
|
|
+ *
|
|
|
+ * @param currentDate 当前时间
|
|
|
+ * @return 返回时间戳
|
|
|
+ */
|
|
|
+ public static Long dateToTimestamp(Date currentDate) {
|
|
|
+ return currentDate.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 时间戳转时间
|
|
|
+ *
|
|
|
+ * @param currentTimestamp 当前时间戳
|
|
|
+ * @return 返回日期
|
|
|
+ */
|
|
|
+ public static Date timestampToDate(Long currentTimestamp) {
|
|
|
+ return new Date(currentTimestamp);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 时间戳转时间
|
|
|
+ *
|
|
|
+ * @param currentTimestamp 当前时间戳
|
|
|
+ * @param pattern 字符串格式
|
|
|
+ * @return 返回日期
|
|
|
+ */
|
|
|
+ public static String timestampToDate(Long currentTimestamp, String pattern) {
|
|
|
+ Date date = new Date(currentTimestamp);
|
|
|
+ return format(date, pattern);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 传入一个时间,获取此时间在tb中属于哪个时间段的读数,则获取的这个时间段的开始时间
|
|
|
+ *
|
|
|
+ * @param currentDate 传入的时间
|
|
|
+ * @param interval 传入读数的频率,不能大于60
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date getBeginTime(Date currentDate, Integer interval) {
|
|
|
+ interval = interval > 60 ? 60 : interval;
|
|
|
+ Calendar currentCalendar = Calendar.getInstance();
|
|
|
+ currentCalendar.setTime(currentDate);
|
|
|
+
|
|
|
+ int beginTimeMinute = currentCalendar.get(Calendar.MINUTE) / interval * interval;
|
|
|
+ currentCalendar.set(Calendar.MINUTE, beginTimeMinute);
|
|
|
+ return currentCalendar.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 传入一个时间,获取此时间在tb中属于哪个时间段的读数,则获取的这个时间段的开始时间
|
|
|
+ *
|
|
|
+ * @param currentDate 传入的时间 yyyy-MM-dd HH:mm:ss
|
|
|
+ * @param interval 传入读数的频率,不能大于60
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date getBeginTime(String currentDate, Integer interval) {
|
|
|
+ return getBeginTime(parse(currentDate, DATE_TIME_PATTERN), interval);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 传入一个时间,获取此时间在tb中属于哪个时间段的读数,则获取的这个时间段的开始时间
|
|
|
+ *
|
|
|
+ * @param currentTimeStamp 传入的时间戳
|
|
|
+ * @param interval 传入读数的频率,不能大于60
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date getBeginTime(Long currentTimeStamp, Integer interval) {
|
|
|
+ return getBeginTime(new Date(currentTimeStamp), interval);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 传入一个时间,获取此时间在tb中属于哪个时间段的读数,则获取的这个时间段的开始时间,间隔时间默认为15
|
|
|
+ *
|
|
|
+ * @param currentDate 传入的时间 yyyy-MM-dd HH:mm:ss
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date getBeginTime(Date currentDate) {
|
|
|
+ return getBeginTime(currentDate, 15);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 传入一个时间,获取此时间在tb中属于哪个时间段的读数,则获取的这个时间段的结束时间
|
|
|
+ *
|
|
|
+ * @param currentDate 传入的时间 yyyy-MM-dd HH:mm:ss
|
|
|
+ * @param interval 传入读数的频率,不能大于60
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date getEndTime(Date currentDate, Integer interval) {
|
|
|
+ interval = interval > 60 ? 60 : interval;
|
|
|
+ Calendar currentCalendar = Calendar.getInstance();
|
|
|
+ currentCalendar.setTime(currentDate);
|
|
|
+
|
|
|
+ int beginTimeMinute = currentCalendar.get(Calendar.MINUTE) / interval * (interval + 1);
|
|
|
+ currentCalendar.set(Calendar.MINUTE, beginTimeMinute);
|
|
|
+ return currentCalendar.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 传入一个时间,获取此时间在tb中属于哪个时间段的读数,则获取的这个时间段的结束时间
|
|
|
+ *
|
|
|
+ * @param currentDate 传入的时间 yyyy-MM-dd HH:mm:ss
|
|
|
+ * @param interval 传入读数的频率,不能大于60
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date getEndTime(String currentDate, Integer interval) {
|
|
|
+ return getEndTime(parse(currentDate, DATE_TIME_PATTERN), interval);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 传入一个时间,获取此时间在tb中属于哪个时间段的读数,则获取的这个时间段的结束时间
|
|
|
+ *
|
|
|
+ * @param currentTimeStamp 传入的时间戳
|
|
|
+ * @param interval 传入读数的频率,不能大于60
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private static Date getEndTime(Long currentTimeStamp, Integer interval) {
|
|
|
+ return getEndTime(new Date(currentTimeStamp), interval);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 传入一个时间,获取此时间在tb中属于哪个时间段的读数,则获取的这个时间段的结束时间,间隔时间默认为15
|
|
|
+ *
|
|
|
+ * @param currentDate 传入的时间 yyyy-MM-dd HH:mm:ss
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private static Date getEndTime(Date currentDate) {
|
|
|
+ return getEndTime(currentDate, 15);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 对日期的【小时】进行加/减
|
|
|
+ *
|
|
|
+ * @param date 日期
|
|
|
+ * @param hours 小时数,负数为减
|
|
|
+ * @return 加/减几小时后的日期
|
|
|
+ */
|
|
|
+ public static String addDateHours(Date date, int hours) {
|
|
|
+ DateTime dateTime = new DateTime(date);
|
|
|
+ return format(dateTime.plusHours(hours).toDate(), DATE_TIME_PATTERN);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 对日期的【天】进行加/减
|
|
|
+ *
|
|
|
+ * @param date 日期
|
|
|
+ * @param days 天数,负数为减
|
|
|
+ * @return 加/减几天后的日期
|
|
|
+ */
|
|
|
+ public static String addDateDays(Date date, int days) {
|
|
|
+ DateTime dateTime = new DateTime(date);
|
|
|
+ return format(dateTime.plusDays(days).toDate(), DATE_TIME_PATTERN);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据传入时间获取时间
|
|
|
+ * @param currentTime
|
|
|
+ * @param granularity
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static SplitedTimeParam getSplitTimeByGranularity(String currentTime, Granularity granularity) {
|
|
|
+ SplitedTimeParam splitedTimeParam = new SplitedTimeParam();
|
|
|
+ Map<String, String> dateMap = new HashMap<>();
|
|
|
+
|
|
|
+ if (CompareUtil.compare(granularity, Granularity.DAY) == 0) {
|
|
|
+ splitedTimeParam.setBeginTime(getDayBeginTime(currentTime));
|
|
|
+ splitedTimeParam.setEndTime(getDayEndTime(currentTime));
|
|
|
+ return splitedTimeParam;
|
|
|
+ }
|
|
|
+ if (CompareUtil.compare(granularity, Granularity.MONTH) == 0) {
|
|
|
+ splitedTimeParam.setBeginTime(getMonthBeginTime(currentTime));
|
|
|
+ splitedTimeParam.setEndTime(getMonthEndTime(currentTime));
|
|
|
+ return splitedTimeParam;
|
|
|
+ }
|
|
|
+ if (CompareUtil.compare(granularity, Granularity.YEAR) == 0) {
|
|
|
+ splitedTimeParam.setBeginTime(getYearBeginTime(currentTime));
|
|
|
+ splitedTimeParam.setEndTime(getYearEndTime(currentTime));
|
|
|
+ return splitedTimeParam;
|
|
|
+ }
|
|
|
+ return splitedTimeParam;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据时间范围获取时间
|
|
|
+ *
|
|
|
+ * @param timeHorizon
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Map<String, String> getDateTimeByTimeHorizon(String timeHorizon) {
|
|
|
+ Date date = Calendar.getInstance().getTime();
|
|
|
+ Map<String, String> dateMap = new HashMap<>();
|
|
|
+ if (StringUtils.equals(timeHorizon, TimeHorizon.CURRENT_DAY.getCode())) {
|
|
|
+ dateMap.put("beginTime", getDayBeginTime(date));
|
|
|
+ dateMap.put("endTime", getDayEndTime(date));
|
|
|
+ return dateMap;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (StringUtils.equals(timeHorizon, TimeHorizon.LAST_HOURS.getCode())) {
|
|
|
+ dateMap.put("beginTime", addDateHours(date, -24));
|
|
|
+ dateMap.put("endTime", format(date, DATE_TIME_PATTERN));
|
|
|
+ return dateMap;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (StringUtils.equals(timeHorizon, TimeHorizon.LAST_DAYS.getCode())) {
|
|
|
+ dateMap.put("beginTime", addDateDays(date, -7));
|
|
|
+ dateMap.put("endTime", format(date, DATE_TIME_PATTERN));
|
|
|
+ return dateMap;
|
|
|
+ }
|
|
|
+ dateMap.put("beginTime", addDateDays(date, -30));
|
|
|
+ dateMap.put("endTime", format(date, DATE_TIME_PATTERN));
|
|
|
+ return dateMap;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|