TimeUtil.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. package com.its.common.util;
  2. import lombok.extern.slf4j.Slf4j;
  3. import java.time.*;
  4. import java.time.format.DateTimeFormatter;
  5. import java.time.temporal.TemporalField;
  6. import java.time.temporal.WeekFields;
  7. import java.util.Calendar;
  8. import java.util.Locale;
  9. @Slf4j
  10. public class TimeUtil {
  11. public static final DateTimeFormatter YYYYMMDDHHMMSS = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withLocale(Locale.getDefault());
  12. public static final DateTimeFormatter YYYYMMDDHHMM = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
  13. public static final DateTimeFormatter YYYYMMDDHH = DateTimeFormatter.ofPattern("yyyy-MM-dd HH");
  14. public static final DateTimeFormatter YYYYMMDD = DateTimeFormatter.ofPattern("yyyy-MM-dd");
  15. private static long timeOffset;//86400000
  16. private TimeUtil() {
  17. }
  18. public static String getDateTimeFormat(LocalDateTime localDateTime, DateTimeFormatter formatter) {
  19. return localDateTime.format(formatter);
  20. }
  21. public static String getDateTimeFormat(long time, DateTimeFormatter formatter) {
  22. LocalDateTime ldt = LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault());
  23. return ldt.format(formatter);
  24. }
  25. public static String getDateTimeFormat(DateTimeFormatter formatter) {
  26. LocalDateTime ldt = LocalDateTime.ofInstant(Instant.ofEpochMilli(currentTimeMillis()), ZoneId.systemDefault());
  27. return ldt.format(formatter);
  28. }
  29. public static String getDateTimeFormat(ZonedDateTime zonedDateTime, DateTimeFormatter formatter) {
  30. return zonedDateTime.format(formatter);
  31. }
  32. public static LocalDateTime getLocalDateTime(String text, DateTimeFormatter formatter) {
  33. try {
  34. return LocalDateTime.parse(text, formatter);
  35. } catch (Exception e) {
  36. log.error("getLocalDateTime: {}", e.getMessage());
  37. }
  38. return null;
  39. }
  40. public static ZonedDateTime getZonedDateTime(String text, DateTimeFormatter formatter) {
  41. try {
  42. LocalDateTime m1 = LocalDateTime.parse(text, formatter);
  43. return m1.atZone(ZoneId.systemDefault());
  44. } catch (Exception e) {
  45. log.error("getLocalDateTime: {}", e.getMessage());
  46. }
  47. return null;
  48. }
  49. public static String getOffToDay(int days, DateTimeFormatter formatter) {
  50. LocalDateTime ldt = LocalDateTime.ofInstant(Instant.ofEpochMilli(currentTimeMillis()), ZoneId.systemDefault());
  51. if (days < 0) {
  52. ldt = ldt.minusDays(-days);
  53. } else {
  54. ldt = ldt.plusDays(days);
  55. }
  56. return ldt.format(formatter);
  57. }
  58. public static long getOffToDayZeroMil(int days) {
  59. try {
  60. ZonedDateTime ldt = ZonedDateTime.ofInstant(Instant.ofEpochMilli(currentTimeMillis()), ZoneId.systemDefault());
  61. if (days < 0) {
  62. ldt = ldt.minusDays(-days);
  63. } else {
  64. ldt = ldt.plusDays(days);
  65. }
  66. ldt = ZonedDateTime.of(ldt.getYear(), ldt.getMonthValue(), ldt.getDayOfMonth(), 0, 0, 0, 0, ZoneId.systemDefault());
  67. return ldt.toInstant().toEpochMilli();
  68. } catch (Exception e) {
  69. log.error("getOffToDayZeroMil: {}", e.getMessage());
  70. }
  71. return 0;
  72. }
  73. public static int getDayOfMonth(long time) {
  74. return LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault()).getDayOfMonth();
  75. }
  76. public static int getDayOfMonth() {
  77. return LocalDateTime.ofInstant(Instant.ofEpochMilli(currentTimeMillis()), ZoneId.systemDefault()).getDayOfMonth();
  78. }
  79. public static int getMinDaysOfMonth() {
  80. return LocalDateTime.ofInstant(Instant.ofEpochMilli(currentTimeMillis()), ZoneId.systemDefault()).getMonth().minLength();
  81. }
  82. public static int getMonth(long time) {
  83. return LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault()).getMonthValue();
  84. }
  85. public static int getMonth() {
  86. return LocalDateTime.ofInstant(Instant.ofEpochMilli(currentTimeMillis()), ZoneId.systemDefault()).getMonthValue();
  87. }
  88. public static int getHour(long time) {
  89. return LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault()).getHour();
  90. }
  91. public static int getHour() {
  92. return LocalDateTime.ofInstant(Instant.ofEpochMilli(currentTimeMillis()), ZoneId.systemDefault()).getHour();
  93. }
  94. public static int getSecond(long time) {
  95. return LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault()).getSecond();
  96. }
  97. public static int getSecond() {
  98. return LocalDateTime.ofInstant(Instant.ofEpochMilli(currentTimeMillis()), ZoneId.systemDefault()).getSecond();
  99. }
  100. public static int getMinute(long time) {
  101. return LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault()).getMinute();
  102. }
  103. public static int getMinute() {
  104. return LocalDateTime.ofInstant(Instant.ofEpochMilli(currentTimeMillis()), ZoneId.systemDefault()).getMinute();
  105. }
  106. public static int getDayOfWeek(long time) {
  107. return LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault()).getDayOfWeek().getValue();
  108. }
  109. public static int getYear(long time) {
  110. return LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault()).getYear();
  111. }
  112. public static int getYear() {
  113. return LocalDateTime.ofInstant(Instant.ofEpochMilli(currentTimeMillis()), ZoneId.systemDefault()).getYear();
  114. }
  115. public static int getDayOfWeek() {
  116. return LocalDateTime.ofInstant(Instant.ofEpochMilli(currentTimeMillis()), ZoneId.systemDefault()).getDayOfWeek().getValue();
  117. }
  118. public static int getDayOfYear(long time) {
  119. return LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault()).getDayOfYear();
  120. }
  121. public static int getDayOfYear() {
  122. return LocalDateTime.ofInstant(Instant.ofEpochMilli(currentTimeMillis()), ZoneId.systemDefault()).getDayOfYear();
  123. }
  124. public static String getNowWeekMondayFormat(DateTimeFormatter formatter) {
  125. LocalDateTime ldt = LocalDateTime.ofInstant(Instant.ofEpochMilli(currentTimeMillis()), ZoneId.systemDefault());
  126. ldt = LocalDateTime.of(ldt.minusDays(ldt.getDayOfWeek().getValue() - 1).toLocalDate(), LocalTime.MIN);
  127. return ldt.format(formatter);
  128. }
  129. public static String getNowMonthFirstDayFormat(DateTimeFormatter formatter) {
  130. LocalDateTime ldt = LocalDateTime.ofInstant(Instant.ofEpochMilli(currentTimeMillis()), ZoneId.systemDefault());
  131. ldt = LocalDateTime.of(ldt.minusDays(ldt.getDayOfMonth() - 1).toLocalDate(), LocalTime.MIN);
  132. return ldt.format(formatter);
  133. }
  134. public static String getNowYearFirstDayFormat(DateTimeFormatter formatter) {
  135. LocalDateTime ldt = LocalDateTime.ofInstant(Instant.ofEpochMilli(currentTimeMillis()), ZoneId.systemDefault());
  136. ldt = LocalDateTime.of(ldt.minusDays(ldt.getDayOfYear() - 1).toLocalDate(), LocalTime.MIN);
  137. return ldt.format(formatter);
  138. }
  139. public static boolean isSameDay(long time1, long time2) {
  140. return Duration.ofMillis(time1).toDays() - Duration.ofMillis(time2).toDays() == 0;
  141. }
  142. public static boolean isSameWeek(long time1, long time2) {
  143. LocalDateTime ldt1 = LocalDateTime.ofInstant(Instant.ofEpochMilli(time1), ZoneId.systemDefault());
  144. LocalDateTime ldt2 = LocalDateTime.ofInstant(Instant.ofEpochMilli(time2), ZoneId.systemDefault());
  145. TemporalField woy = WeekFields.of(Locale.getDefault()).weekOfWeekBasedYear();
  146. return ldt1.getYear() == ldt2.getYear() && ldt1.get(woy) == ldt2.get(woy);
  147. }
  148. public static boolean isSameMonth(long time1, long time2) {
  149. LocalDateTime ldt1 = LocalDateTime.ofInstant(Instant.ofEpochMilli(time1), ZoneId.systemDefault());
  150. LocalDateTime ldt2 = LocalDateTime.ofInstant(Instant.ofEpochMilli(time2), ZoneId.systemDefault());
  151. return ldt1.getYear() == ldt2.getYear() && ldt1.getMonthValue() == ldt2.getMonthValue();
  152. }
  153. public static boolean isSameQuarter(long time1, long time2) {
  154. LocalDateTime ldt1 = LocalDateTime.ofInstant(Instant.ofEpochMilli(time1), ZoneId.systemDefault());
  155. LocalDateTime ldt2 = LocalDateTime.ofInstant(Instant.ofEpochMilli(time2), ZoneId.systemDefault());
  156. return ldt1.getYear() == ldt2.getYear() && ldt1.getMonthValue() / 4 == ldt2.getMonthValue() / 4;
  157. }
  158. public static boolean isSameYear(long time1, long time2) {
  159. return LocalDateTime.ofInstant(Instant.ofEpochMilli(time1), ZoneId.systemDefault()).getYear()
  160. == LocalDateTime.ofInstant(Instant.ofEpochMilli(time2), ZoneId.systemDefault()).getYear();
  161. }
  162. public static boolean isToDay(long time) {
  163. return isSameDay(time, currentTimeMillis());
  164. }
  165. public static long dayOffsetNow(long time) {
  166. return Duration.ofMillis(currentTimeMillis()).toDays() - Duration.ofMillis(time).toDays();
  167. }
  168. public static long dayOffset(long time1, long time2) {
  169. return Duration.ofMillis(time1).toDays() - Duration.ofMillis(time2).toDays();
  170. }
  171. public static long currentTimeMillis() {
  172. return Clock.systemDefaultZone().instant().toEpochMilli() + timeOffset;
  173. }
  174. public static long offsetCurrentTimeMillis(int offsetDays, int hour, int minute, int secord) {
  175. ZonedDateTime ldt = ZonedDateTime.ofInstant(Instant.ofEpochMilli(currentTimeMillis()), ZoneId.systemDefault());
  176. if (offsetDays > 0) {
  177. ldt = ldt.plusDays(offsetDays);
  178. } else if (offsetDays < 0) {
  179. ldt = ldt.minusDays(offsetDays);
  180. }
  181. ldt = ldt.withHour(hour);
  182. ldt = ldt.withMinute(minute);
  183. ldt = ldt.withSecond(secord);
  184. return ldt.toEpochSecond() * 1000;
  185. }
  186. public static long epochSecond() {
  187. return Clock.systemDefaultZone().instant().getEpochSecond() + timeOffset / 1000;
  188. }
  189. public static boolean setCurrentDateTime(String datetime) {
  190. ZonedDateTime zdt = getZonedDateTime(datetime, YYYYMMDDHHMMSS);
  191. if (zdt == null) {
  192. zdt = getZonedDateTime(datetime, YYYYMMDDHHMM);
  193. }
  194. if (zdt == null) {
  195. zdt = getZonedDateTime(datetime, YYYYMMDDHH);
  196. }
  197. if (zdt == null) {
  198. zdt = getZonedDateTime(datetime, YYYYMMDD);
  199. }
  200. if (zdt == null) {
  201. return false;
  202. }
  203. timeOffset = zdt.toEpochSecond() * 1000 - Clock.systemDefaultZone().instant().toEpochMilli();
  204. return true;
  205. }
  206. public static ZonedDateTime getZonedDateTime(String datetime) {
  207. ZonedDateTime zdt = getZonedDateTime(datetime, YYYYMMDDHHMMSS);
  208. if (zdt == null) {
  209. zdt = getZonedDateTime(datetime, YYYYMMDDHHMM);
  210. }
  211. if (zdt == null) {
  212. zdt = getZonedDateTime(datetime, YYYYMMDDHH);
  213. }
  214. if (zdt == null) {
  215. zdt = getZonedDateTime(datetime, YYYYMMDD);
  216. }
  217. return zdt;
  218. }
  219. public static final ZoneOffset ZONE_OFFSET = ZoneOffset.ofTotalSeconds(Calendar.getInstance().getTimeZone().getRawOffset() / 1000);
  220. public static LocalDate getLocalDate() {
  221. long currentTimeMillis = currentTimeMillis();
  222. LocalDateTime ldt = LocalDateTime.ofEpochSecond(currentTimeMillis / 1000, 0, ZONE_OFFSET);
  223. return ldt.toLocalDate();
  224. }
  225. public static LocalDateTime getLocalDateTime() {
  226. long currentTimeMillis = currentTimeMillis();
  227. return LocalDateTime.ofEpochSecond(currentTimeMillis / 1000, 0, ZONE_OFFSET);
  228. }
  229. public static long getDayOfSecond() {
  230. LocalDateTime localDateTime = getLocalDateTime();
  231. int hour = localDateTime.getHour();
  232. int minute = localDateTime.getMinute();
  233. long second = localDateTime.getSecond();
  234. second = hour * 60 * 60 + minute * 60 + second;
  235. return second;
  236. }
  237. public static long getDayOfMinute() {
  238. LocalDateTime localDateTime = getLocalDateTime();
  239. int hour = localDateTime.getHour();
  240. int minute = localDateTime.getMinute();
  241. minute = hour * 60 + minute;
  242. return minute;
  243. }
  244. public static long getTimeByLocal(int year, int month, int day, int hour, int minte, int second) {
  245. return LocalDateTime.of(year, month, month, hour, minte, second).atZone(ZONE_OFFSET).toInstant().toEpochMilli();
  246. }
  247. public static int getSecondByDay(long currentTimeMillis) {
  248. int hour = getHour(currentTimeMillis);
  249. int minute = getMinute(currentTimeMillis);
  250. int second = getSecond(currentTimeMillis);
  251. return hour * 3600 + minute * 60 + second;
  252. }
  253. public static int getSecondByDay() {
  254. int hour = getHour();
  255. int minute = getMinute();
  256. int second = getSecond();
  257. return hour * 3600 + minute * 60 + second;
  258. }
  259. public static long nanoTime () {
  260. return System.nanoTime();
  261. }
  262. }