public class TimeUtil {
private final static String DATATIMEFORMATE = "yyyy-MM-dd HH:mm:ss";
private final static String DATATIMEFORMATET = "yyyy-MM-dd'T'HH:mm:ss";
private final static String DATATIMEFORMATETZ = "yyyy-MM-dd'T'HH:mm:ss'Z'";
private UmeTimeUtil() {
}
public static String timestamToDatetime(long timestamp) {
Instant instant = Instant.ofEpochMilli(timestamp);
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
String format = localDateTime + "Z";
return format;
}
private static DateTimeFormatter getDateTimeFormat() {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(DATATIMEFORMATE);
return dateTimeFormatter;
}
public static String getNowDateTime() {
LocalDateTime now = LocalDateTime.now();
String NowDateTime = now.format(getDateTimeFormat());
return NowDateTime;
}
public static String getNowDateTimeWithTAndZ() {
LocalDateTime now = LocalDateTime.now();
String NowDateTime = now + "Z";
return NowDateTime;
}
public static String getNowDivDateTime(long day) {
LocalDateTime now = LocalDateTime.now();
LocalDateTime localDateTime = now.minusDays(day);
String NowDateTime = localDateTime.toString();
return NowDateTime + "Z";
}
public static String getNowDivHourTime(long hours) {
LocalDateTime now = LocalDateTime.now();
LocalDateTime localDateTime = now.minusHours(hours);
String NowDateTime = localDateTime.toString();
return NowDateTime + "Z";
}
public static String getZeroToNowTodayMinute() {
LocalDateTime dateNow = LocalDateTime.now();
LocalDateTime dateStart = LocalDateTime.of(LocalDateTime.now().getYear(), LocalDateTime.now().getMonth(), LocalDateTime.now().getDayOfMonth(), 0, 0);
Duration between = Duration.between(dateStart, dateNow);
long diffMinutes = between.toMinutes();
if (diffMinutes == 0L) {
diffMinutes = 1L;
}
return diffMinutes + "m";
}
public static String getFormatePrometheusDate(String dateTime) {
LocalDateTime startDateFormate = LocalDateTime.parse(dateTime, DateTimeFormatter.ofPattern(DATATIMEFORMATE));
String format = startDateFormate.format(DateTimeFormatter.ofPattern(DATATIMEFORMATETZ));
return format;
}
public static String getDiffStartTimeAndEndDateTime(String startDate, String endDate) {
LocalDateTime now = LocalDateTime.now();
LocalDateTime startDateFormate = LocalDateTime.parse(startDate, DateTimeFormatter.ofPattern(DATATIMEFORMATE));
LocalDateTime endDateFormate = LocalDateTime.parse(endDate, DateTimeFormatter.ofPattern(DATATIMEFORMATE));
if (now.isBefore(endDateFormate)) {
endDateFormate = now;
}
Duration between = Duration.between(startDateFormate, endDateFormate);
long days = between.toDays();
if (days > 90L) {
return "1w";
} else if (days > 30L) {
return "1w";
} else if (days > 6L) {
return "1d";
} else {
return "1h";
}
}
}
/**
* 获取上周的前后时间
* @return 今天周二(0314) => 20230306,20230313
*/
public List<String> getCurrentWeek() {
List<String> list = new ArrayList<>();
DateTimeFormatter as = DateTimeFormatter.ofPattern("yyyyMMdd");
LocalDateTime startMonth = LocalDateTime.now();
DayOfWeek dayOfWeek = startMonth.getDayOfWeek();
int value = dayOfWeek.getValue();
LocalDateTime end = startMonth.minusDays(value-1);
LocalDateTime start = startMonth.minusDays(value+6);
String startTime = start.format(as);
String endTime = end.format(as);
list.add(0, startTime);
list.add(1, endTime);
log.info("the getCurrentWeek is :" + JSONObject.toJSONString(list));
return list;
}
/**
* 获取当月时间
* @return [20230301,20230401]
*/
public List<String> getCurrentMonth() {
List<String> list = new ArrayList<>();
DateTimeFormatter as = DateTimeFormatter.ofPattern("yyyyMM");
LocalDateTime startMonth = LocalDateTime.now();
LocalDateTime endMonth = startMonth.plusMonths(1L);
String startTime = startMonth.format(as) + "01";
String endTime = endMonth.format(as) + "01";
list.add(0, startTime);
list.add(1, endTime);
log.info("the getCurrentMonth is :" + JSONObject.toJSONString(list));
return list;
}
/**
* 获取当天时间
* @return 20230314
*/
public String getCurrentDate() {
DateTimeFormatter as = DateTimeFormatter.ofPattern("yyyyMMdd");
LocalDateTime now = LocalDateTime.now();
String format = now.format(as);
log.info("the getCurrentDate is :" + format);
return format;
}
/**
* 获取当前时间
* @return 2023022813
*/
private String getCurrentTime() {
DateTimeFormatter as = DateTimeFormatter.ofPattern("yyyyMMddHH");
LocalDateTime now = LocalDateTime.now();
LocalDateTime localDateTime = now.minusHours(1L);
String format = localDateTime.format(as);
log.info("the getCurrentTime is :" + format);
return format;
}