初始化代码
This commit is contained in:
@@ -0,0 +1,210 @@
|
||||
|
||||
package util.time;
|
||||
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.*;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Date;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import static additive.ValidTool.assertIsTrue;
|
||||
|
||||
public class DateTimeUtil {
|
||||
|
||||
private static final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd";
|
||||
private static final String DEFAULT_TIME_PATTERN = "HH:mm:ss";
|
||||
private static final String DEFAULT_PATTERN = "yyyy-MM-dd HH:mm:ss";
|
||||
private static final String DEFAULT_PATTERN_CLOSE = "yyyyMMddHHmmss";
|
||||
private static final String DATE_TIME_MINUTE_PATTERN = "yyyy-MM-dd HH:mm";
|
||||
private static final String ISO_8601_PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSSZZ";
|
||||
private static final String lO_G_PARAM_TIME_PATTERN = "yyyy-MM-dd-HHmm";
|
||||
|
||||
|
||||
/**
|
||||
* 获取当天日期,"yyyy-MM-dd"
|
||||
*/
|
||||
public static String getCurrentDate() {
|
||||
return LocalDate.now().toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前时间,yyyy-MM-dd HH:mm:ss
|
||||
*/
|
||||
public static String getCurrentDateTime() {
|
||||
return LocalDateTime.now().format(DateTimeFormatter.ofPattern(DEFAULT_PATTERN));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取与所给日期间隔要求天数的日期
|
||||
* @param localDate 参考日期, yyyy-MM-dd
|
||||
* @param dayOffset 间隔天数
|
||||
*/
|
||||
public static String plusDay(final String localDate, final int dayOffset) {
|
||||
assertIsTrue(dayOffset >= 0, "偏移量必须大于0");
|
||||
return LocalDate.parse(localDate).plusDays(dayOffset).format(DateTimeFormatter.ISO_LOCAL_DATE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取与所给日期间隔要求天数的日期
|
||||
* @param localDate 参考日期, yyyy-MM-dd
|
||||
* @param dayOffset 间隔天数
|
||||
*/
|
||||
public static String minusDay(final String localDate, final int dayOffset) {
|
||||
assertIsTrue(dayOffset >= 0, "偏移量必须大于0");
|
||||
return LocalDate.parse(localDate).minusDays(dayOffset).format(DateTimeFormatter.ISO_LOCAL_DATE);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 将日期时间转换成指定格式的时间
|
||||
*/
|
||||
public static String dateToString(String pattern, LocalDateTime dateTime) {
|
||||
return DateTimeFormatter.ofPattern(pattern).format(dateTime);
|
||||
}
|
||||
|
||||
/**
|
||||
* 时间戳转换为指定格式字符串(秒)
|
||||
*/
|
||||
public static String dateToString(String pattern, long epochSecond, TimeZone timeZone) {
|
||||
return dateToString(pattern, LocalDateTime.ofInstant(Instant.ofEpochSecond(epochSecond), timeZone.toZoneId()));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 时间戳转成指定格式字符串(毫秒)
|
||||
*/
|
||||
public static String timestampToString(String pattern, long timestamp, TimeZone timeZone) {
|
||||
return dateToString(pattern, LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), timeZone.toZoneId()));
|
||||
}
|
||||
|
||||
public static String dateToString(String pattern, ZonedDateTime dateTime) {
|
||||
return DateTimeFormatter.ofPattern(pattern)
|
||||
.format(dateTime);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将LocalDateTime转换成指定格式字符串
|
||||
*/
|
||||
public static String localTimeToString(LocalDateTime localDateTime, String pattern) {
|
||||
LocalTime localTime = localDateTime.toLocalTime();
|
||||
return DateTimeFormatter.ofPattern(pattern).format(localTime);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将时间格式换成"HH:mm:ss" 格式
|
||||
*/
|
||||
public static String formatDateToTime(LocalDateTime date) {
|
||||
return DateTimeUtil.dateToString(DEFAULT_TIME_PATTERN, date);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将时间格式换成"yyyy-MM-dd" 格式
|
||||
*/
|
||||
public static String formatDateToDay(LocalDateTime date) {
|
||||
return DateTimeUtil.dateToString(DEFAULT_DATE_PATTERN, date);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param timeString yyyy-MM-dd-HHmm
|
||||
* @return yyyy-MM-dd HH:mm 格式的时间字符串
|
||||
*/
|
||||
public static String paramTimeFormat(String timeString) throws ParseException {
|
||||
Date newDate = new SimpleDateFormat(lO_G_PARAM_TIME_PATTERN).parse(timeString);
|
||||
return new SimpleDateFormat(DATE_TIME_MINUTE_PATTERN).format(newDate);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取当前时间,yyyy-MM-dd HH:mm:ss
|
||||
* 注意:使用当前系统默认的时区
|
||||
*/
|
||||
public static LocalDateTime getNow() {
|
||||
return LocalDateTime.now();
|
||||
}
|
||||
|
||||
/**
|
||||
* 将时间戳(单位:毫秒)转成 LocalDateTime
|
||||
*/
|
||||
public static LocalDateTime timeStampToLocalDateTime(long timeStamp) {
|
||||
return Instant.ofEpochMilli(timeStamp).atZone(ZoneId.systemDefault()).toLocalDateTime();
|
||||
}
|
||||
|
||||
/**
|
||||
* 将时间戳(单位为:秒)转成 LocalDateTime
|
||||
*/
|
||||
public static LocalDateTime epochSecondToLocalDateTime(long epochSecond, TimeZone timeZone) {
|
||||
return epochSecondToLocalDateTime(epochSecond, timeZone.toZoneId());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 将时间戳(单位为:秒)转成 LocalDateTime
|
||||
*/
|
||||
public static LocalDateTime epochSecondToLocalDateTime(long epochSecond, ZoneId zoneId) {
|
||||
return Instant.ofEpochSecond(epochSecond).atZone(zoneId).toLocalDateTime();
|
||||
}
|
||||
|
||||
/**
|
||||
* LocalDateTime转成时间戳:毫秒
|
||||
*/
|
||||
public static long toEpochMilli(LocalDateTime localDateTime, TimeZone timeZone) {
|
||||
return Instant.from(localDateTime.atZone(timeZone.toZoneId())).toEpochMilli();
|
||||
}
|
||||
|
||||
/**
|
||||
* 将字符串转换为LocalDateTime
|
||||
*/
|
||||
public static LocalDateTime stringToLocalDateTime(String dateStr, String pattern) {
|
||||
DateTimeFormatter df = DateTimeFormatter.ofPattern(pattern);
|
||||
return LocalDateTime.parse(dateStr, df);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 到天的字符串,转成LocalDateTime
|
||||
* @param dayStr 如"2017-11-16"
|
||||
* @return 返回0点0分0秒的时间 如 "2017-11-16 00:00:00"
|
||||
*/
|
||||
public static LocalDateTime dayStringToLocalDateTime(String dayStr) {
|
||||
return LocalDate.parse(dayStr, DateTimeFormatter.ofPattern(DEFAULT_DATE_PATTERN)).atTime(0, 0, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* LocalDateTime转成时间戳:秒
|
||||
*/
|
||||
public static long toEpochSecond(LocalDateTime localDateTime, TimeZone timeZone) {
|
||||
return Instant.from(localDateTime.atZone(timeZone.toZoneId())).getEpochSecond();
|
||||
}
|
||||
|
||||
|
||||
public static long toEpochSecond(LocalDateTime localDateTime, ZoneId zoneId) {
|
||||
return localDateTime.atZone(zoneId).toEpochSecond();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取当前时间的秒数
|
||||
*/
|
||||
public static long getNowSeconds(){
|
||||
return
|
||||
LocalDateTime.now().toEpochSecond(ZoneOffset.MAX);
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否同一个时间点
|
||||
*/
|
||||
public static boolean isTheSameDay(LocalDateTime startDate, LocalDateTime endDate) {
|
||||
return startDate.toLocalDate()
|
||||
.isEqual(endDate.toLocalDate());
|
||||
}
|
||||
|
||||
/**
|
||||
* LocalDateTime 转成时间戳
|
||||
*/
|
||||
public static Instant toInstant(LocalDateTime endDate, TimeZone timeZone) {
|
||||
return endDate.atZone(timeZone.toZoneId()).toInstant();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
|
||||
|
||||
package util.time;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import entity.OriginTimeValue;
|
||||
import entity.TargetTimeValue;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.LocalTime;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 补时间点工具类
|
||||
* 之前在ws上班的时候,处理图标数据会遇到某些点没有数据的情况,就需要对这些时间点进行补点处理
|
||||
*
|
||||
* @author huangzj
|
||||
* @date 2018-11-23 22:43:36
|
||||
*/
|
||||
public enum FillFullTimeValueUtil {
|
||||
/**
|
||||
* 分钟粒度
|
||||
*/
|
||||
MINUTE {
|
||||
@Override
|
||||
public List<TargetTimeValue> fullFillTimeValue(LocalDateTime startDate, LocalDateTime endDate, int timeInterval,
|
||||
List<OriginTimeValue> originTimeValues, TimeZone timeZone) {
|
||||
//Myview 向前聚合,返回前XX秒为最开始计数时间
|
||||
Instant firstInstant = toInstant(startDate, timeZone);
|
||||
Instant finalInstant = toInstant(endDate, timeZone);
|
||||
//补时间点--规则一样,间隔转换成秒
|
||||
return fullFillRule(originTimeValues, firstInstant, finalInstant, timeInterval * 60);
|
||||
}
|
||||
},
|
||||
/**
|
||||
* 小时粒度
|
||||
*/
|
||||
HOUR {
|
||||
@Override
|
||||
public List<TargetTimeValue> fullFillTimeValue(LocalDateTime startDate, LocalDateTime endDate, int timeInterval,
|
||||
List<OriginTimeValue> originTimeValues, TimeZone timeZone) {
|
||||
Instant firstInstant = toInstant(startDate, timeZone);
|
||||
Instant finalInstant = toInstant(endDate, timeZone);
|
||||
//补时间点--规则一样
|
||||
return fullFillRule(originTimeValues, firstInstant, finalInstant, timeInterval * 60 * 60);
|
||||
}
|
||||
},
|
||||
/**
|
||||
* 天粒度
|
||||
*/
|
||||
DAY {
|
||||
@Override
|
||||
public List<TargetTimeValue> fullFillTimeValue(LocalDateTime startDate, LocalDateTime endDate, int timeInterval,
|
||||
List<OriginTimeValue> originTimeValues, TimeZone timeZone) {
|
||||
//区别,把时间转成天
|
||||
Instant firstInstant = toInstant(LocalDateTime.of(startDate.toLocalDate(), LocalTime.of(0, 0, 0)), timeZone);
|
||||
Instant finalInstant = toInstant(LocalDateTime.of(endDate.toLocalDate(), LocalTime.of(0, 0, 0)), timeZone);
|
||||
//补时间点--规则一样
|
||||
return fullFillRule(originTimeValues, firstInstant, finalInstant, timeInterval * 60 * 60 * 24);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* 抽象方法,实现补充时间点
|
||||
* @param startDate 开始时间 格式如:yyyy-MM-dd 00:00:00
|
||||
* @param endDate 结束时间:格式如:yyyy-MM-dd 23:59:59
|
||||
* @param timeInterval 数据采集粒度,单位:分钟、小时、天
|
||||
* @param originTimeValues 缺少时间点对象(统一处理成long、long)
|
||||
* @param timeZone 时区
|
||||
* @return 补时间点后对象,没有补null
|
||||
*/
|
||||
public abstract List<TargetTimeValue> fullFillTimeValue(LocalDateTime startDate, LocalDateTime endDate,
|
||||
int timeInterval, List<OriginTimeValue> originTimeValues, TimeZone timeZone);
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @param dateTime 时间
|
||||
* @param timeZone 时区
|
||||
* @return Instant,和TimeStamp类似
|
||||
*/
|
||||
private static Instant toInstant(LocalDateTime dateTime, TimeZone timeZone) {
|
||||
return dateTime.atZone(timeZone.toZoneId())
|
||||
.toInstant();
|
||||
}
|
||||
|
||||
|
||||
private static List<TargetTimeValue> fullFillRule(List<OriginTimeValue> originTimeValues, Instant firstInstant, Instant finalInstant,
|
||||
int seconds) {
|
||||
//转换为Time-value形式的map
|
||||
if (CollectionUtils.isEmpty(originTimeValues)) {
|
||||
originTimeValues = Lists.newArrayList();
|
||||
}
|
||||
Map<Long, Long> timeValueMap = originTimeValues.stream().collect(Collectors.toMap(OriginTimeValue::getTime,
|
||||
OriginTimeValue::getValue));
|
||||
final List<TargetTimeValue> resultList = new ArrayList<>();
|
||||
|
||||
Instant cursorInstant = firstInstant;
|
||||
//循环按照时间粒度补充时间点。
|
||||
while (cursorInstant.isBefore(finalInstant) || cursorInstant.equals(finalInstant)) {
|
||||
long epochSecond = cursorInstant.getEpochSecond();
|
||||
TargetTimeValue timeValueInRule = new TargetTimeValue();
|
||||
timeValueInRule.setValue(timeValueMap.getOrDefault(epochSecond, null));
|
||||
timeValueInRule.setTime(epochSecond);
|
||||
resultList.add(timeValueInRule);
|
||||
|
||||
//移动到下一个数据点
|
||||
cursorInstant = cursorInstant.plusSeconds(seconds);
|
||||
}
|
||||
//按时间排序,最新的排在最后面
|
||||
return resultList.parallelStream()
|
||||
.sorted(Comparator.comparing(TargetTimeValue::getTime))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user