package com.its.common.util; import lombok.extern.slf4j.Slf4j; import java.time.*; import java.time.format.DateTimeFormatter; import java.time.temporal.TemporalField; import java.time.temporal.WeekFields; import java.util.Calendar; import java.util.Locale; @Slf4j public class TimeUtil { public static final DateTimeFormatter YYYYMMDDHHMMSS = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withLocale(Locale.getDefault()); public static final DateTimeFormatter YYYYMMDDHHMM = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"); public static final DateTimeFormatter YYYYMMDDHH = DateTimeFormatter.ofPattern("yyyy-MM-dd HH"); public static final DateTimeFormatter YYYYMMDD = DateTimeFormatter.ofPattern("yyyy-MM-dd"); private static long timeOffset;//86400000 private TimeUtil() { } public static String getDateTimeFormat(LocalDateTime localDateTime, DateTimeFormatter formatter) { return localDateTime.format(formatter); } public static String getDateTimeFormat(long time, DateTimeFormatter formatter) { LocalDateTime ldt = LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault()); return ldt.format(formatter); } public static String getDateTimeFormat(DateTimeFormatter formatter) { LocalDateTime ldt = LocalDateTime.ofInstant(Instant.ofEpochMilli(currentTimeMillis()), ZoneId.systemDefault()); return ldt.format(formatter); } public static String getDateTimeFormat(ZonedDateTime zonedDateTime, DateTimeFormatter formatter) { return zonedDateTime.format(formatter); } public static LocalDateTime getLocalDateTime(String text, DateTimeFormatter formatter) { try { return LocalDateTime.parse(text, formatter); } catch (Exception e) { log.error("getLocalDateTime: {}", e.getMessage()); } return null; } public static ZonedDateTime getZonedDateTime(String text, DateTimeFormatter formatter) { try { LocalDateTime m1 = LocalDateTime.parse(text, formatter); return m1.atZone(ZoneId.systemDefault()); } catch (Exception e) { log.error("getLocalDateTime: {}", e.getMessage()); } return null; } public static String getOffToDay(int days, DateTimeFormatter formatter) { LocalDateTime ldt = LocalDateTime.ofInstant(Instant.ofEpochMilli(currentTimeMillis()), ZoneId.systemDefault()); if (days < 0) { ldt = ldt.minusDays(-days); } else { ldt = ldt.plusDays(days); } return ldt.format(formatter); } public static long getOffToDayZeroMil(int days) { try { ZonedDateTime ldt = ZonedDateTime.ofInstant(Instant.ofEpochMilli(currentTimeMillis()), ZoneId.systemDefault()); if (days < 0) { ldt = ldt.minusDays(-days); } else { ldt = ldt.plusDays(days); } ldt = ZonedDateTime.of(ldt.getYear(), ldt.getMonthValue(), ldt.getDayOfMonth(), 0, 0, 0, 0, ZoneId.systemDefault()); return ldt.toInstant().toEpochMilli(); } catch (Exception e) { log.error("getOffToDayZeroMil: {}", e.getMessage()); } return 0; } public static int getDayOfMonth(long time) { return LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault()).getDayOfMonth(); } public static int getDayOfMonth() { return LocalDateTime.ofInstant(Instant.ofEpochMilli(currentTimeMillis()), ZoneId.systemDefault()).getDayOfMonth(); } public static int getMinDaysOfMonth() { return LocalDateTime.ofInstant(Instant.ofEpochMilli(currentTimeMillis()), ZoneId.systemDefault()).getMonth().minLength(); } public static int getMonth(long time) { return LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault()).getMonthValue(); } public static int getMonth() { return LocalDateTime.ofInstant(Instant.ofEpochMilli(currentTimeMillis()), ZoneId.systemDefault()).getMonthValue(); } public static int getHour(long time) { return LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault()).getHour(); } public static int getHour() { return LocalDateTime.ofInstant(Instant.ofEpochMilli(currentTimeMillis()), ZoneId.systemDefault()).getHour(); } public static int getSecond(long time) { return LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault()).getSecond(); } public static int getSecond() { return LocalDateTime.ofInstant(Instant.ofEpochMilli(currentTimeMillis()), ZoneId.systemDefault()).getSecond(); } public static int getMinute(long time) { return LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault()).getMinute(); } public static int getMinute() { return LocalDateTime.ofInstant(Instant.ofEpochMilli(currentTimeMillis()), ZoneId.systemDefault()).getMinute(); } public static int getDayOfWeek(long time) { return LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault()).getDayOfWeek().getValue(); } public static int getYear(long time) { return LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault()).getYear(); } public static int getYear() { return LocalDateTime.ofInstant(Instant.ofEpochMilli(currentTimeMillis()), ZoneId.systemDefault()).getYear(); } public static int getDayOfWeek() { return LocalDateTime.ofInstant(Instant.ofEpochMilli(currentTimeMillis()), ZoneId.systemDefault()).getDayOfWeek().getValue(); } public static int getDayOfYear(long time) { return LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault()).getDayOfYear(); } public static int getDayOfYear() { return LocalDateTime.ofInstant(Instant.ofEpochMilli(currentTimeMillis()), ZoneId.systemDefault()).getDayOfYear(); } public static String getNowWeekMondayFormat(DateTimeFormatter formatter) { LocalDateTime ldt = LocalDateTime.ofInstant(Instant.ofEpochMilli(currentTimeMillis()), ZoneId.systemDefault()); ldt = LocalDateTime.of(ldt.minusDays(ldt.getDayOfWeek().getValue() - 1).toLocalDate(), LocalTime.MIN); return ldt.format(formatter); } public static String getNowMonthFirstDayFormat(DateTimeFormatter formatter) { LocalDateTime ldt = LocalDateTime.ofInstant(Instant.ofEpochMilli(currentTimeMillis()), ZoneId.systemDefault()); ldt = LocalDateTime.of(ldt.minusDays(ldt.getDayOfMonth() - 1).toLocalDate(), LocalTime.MIN); return ldt.format(formatter); } public static String getNowYearFirstDayFormat(DateTimeFormatter formatter) { LocalDateTime ldt = LocalDateTime.ofInstant(Instant.ofEpochMilli(currentTimeMillis()), ZoneId.systemDefault()); ldt = LocalDateTime.of(ldt.minusDays(ldt.getDayOfYear() - 1).toLocalDate(), LocalTime.MIN); return ldt.format(formatter); } public static boolean isSameDay(long time1, long time2) { return Duration.ofMillis(time1).toDays() - Duration.ofMillis(time2).toDays() == 0; } public static boolean isSameWeek(long time1, long time2) { LocalDateTime ldt1 = LocalDateTime.ofInstant(Instant.ofEpochMilli(time1), ZoneId.systemDefault()); LocalDateTime ldt2 = LocalDateTime.ofInstant(Instant.ofEpochMilli(time2), ZoneId.systemDefault()); TemporalField woy = WeekFields.of(Locale.getDefault()).weekOfWeekBasedYear(); return ldt1.getYear() == ldt2.getYear() && ldt1.get(woy) == ldt2.get(woy); } public static boolean isSameMonth(long time1, long time2) { LocalDateTime ldt1 = LocalDateTime.ofInstant(Instant.ofEpochMilli(time1), ZoneId.systemDefault()); LocalDateTime ldt2 = LocalDateTime.ofInstant(Instant.ofEpochMilli(time2), ZoneId.systemDefault()); return ldt1.getYear() == ldt2.getYear() && ldt1.getMonthValue() == ldt2.getMonthValue(); } public static boolean isSameQuarter(long time1, long time2) { LocalDateTime ldt1 = LocalDateTime.ofInstant(Instant.ofEpochMilli(time1), ZoneId.systemDefault()); LocalDateTime ldt2 = LocalDateTime.ofInstant(Instant.ofEpochMilli(time2), ZoneId.systemDefault()); return ldt1.getYear() == ldt2.getYear() && ldt1.getMonthValue() / 4 == ldt2.getMonthValue() / 4; } public static boolean isSameYear(long time1, long time2) { return LocalDateTime.ofInstant(Instant.ofEpochMilli(time1), ZoneId.systemDefault()).getYear() == LocalDateTime.ofInstant(Instant.ofEpochMilli(time2), ZoneId.systemDefault()).getYear(); } public static boolean isToDay(long time) { return isSameDay(time, currentTimeMillis()); } public static long dayOffsetNow(long time) { return Duration.ofMillis(currentTimeMillis()).toDays() - Duration.ofMillis(time).toDays(); } public static long dayOffset(long time1, long time2) { return Duration.ofMillis(time1).toDays() - Duration.ofMillis(time2).toDays(); } public static long currentTimeMillis() { return Clock.systemDefaultZone().instant().toEpochMilli() + timeOffset; } public static long offsetCurrentTimeMillis(int offsetDays, int hour, int minute, int secord) { ZonedDateTime ldt = ZonedDateTime.ofInstant(Instant.ofEpochMilli(currentTimeMillis()), ZoneId.systemDefault()); if (offsetDays > 0) { ldt = ldt.plusDays(offsetDays); } else if (offsetDays < 0) { ldt = ldt.minusDays(offsetDays); } ldt = ldt.withHour(hour); ldt = ldt.withMinute(minute); ldt = ldt.withSecond(secord); return ldt.toEpochSecond() * 1000; } public static long epochSecond() { return Clock.systemDefaultZone().instant().getEpochSecond() + timeOffset / 1000; } public static boolean setCurrentDateTime(String datetime) { ZonedDateTime zdt = getZonedDateTime(datetime, YYYYMMDDHHMMSS); if (zdt == null) { zdt = getZonedDateTime(datetime, YYYYMMDDHHMM); } if (zdt == null) { zdt = getZonedDateTime(datetime, YYYYMMDDHH); } if (zdt == null) { zdt = getZonedDateTime(datetime, YYYYMMDD); } if (zdt == null) { return false; } timeOffset = zdt.toEpochSecond() * 1000 - Clock.systemDefaultZone().instant().toEpochMilli(); return true; } public static ZonedDateTime getZonedDateTime(String datetime) { ZonedDateTime zdt = getZonedDateTime(datetime, YYYYMMDDHHMMSS); if (zdt == null) { zdt = getZonedDateTime(datetime, YYYYMMDDHHMM); } if (zdt == null) { zdt = getZonedDateTime(datetime, YYYYMMDDHH); } if (zdt == null) { zdt = getZonedDateTime(datetime, YYYYMMDD); } return zdt; } public static final ZoneOffset ZONE_OFFSET = ZoneOffset.ofTotalSeconds(Calendar.getInstance().getTimeZone().getRawOffset() / 1000); public static LocalDate getLocalDate() { long currentTimeMillis = currentTimeMillis(); LocalDateTime ldt = LocalDateTime.ofEpochSecond(currentTimeMillis / 1000, 0, ZONE_OFFSET); return ldt.toLocalDate(); } public static LocalDateTime getLocalDateTime() { long currentTimeMillis = currentTimeMillis(); return LocalDateTime.ofEpochSecond(currentTimeMillis / 1000, 0, ZONE_OFFSET); } public static long getDayOfSecond() { LocalDateTime localDateTime = getLocalDateTime(); int hour = localDateTime.getHour(); int minute = localDateTime.getMinute(); long second = localDateTime.getSecond(); second = hour * 60 * 60 + minute * 60 + second; return second; } public static long getDayOfMinute() { LocalDateTime localDateTime = getLocalDateTime(); int hour = localDateTime.getHour(); int minute = localDateTime.getMinute(); minute = hour * 60 + minute; return minute; } public static long getTimeByLocal(int year, int month, int day, int hour, int minte, int second) { return LocalDateTime.of(year, month, month, hour, minte, second).atZone(ZONE_OFFSET).toInstant().toEpochMilli(); } public static int getSecondByDay(long currentTimeMillis) { int hour = getHour(currentTimeMillis); int minute = getMinute(currentTimeMillis); int second = getSecond(currentTimeMillis); return hour * 3600 + minute * 60 + second; } public static int getSecondByDay() { int hour = getHour(); int minute = getMinute(); int second = getSecond(); return hour * 3600 + minute * 60 + second; } public static long nanoTime () { return System.nanoTime(); } }