TimeUtil.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package com.example.integratedingtalkoa.util;
  2. import java.time.LocalDateTime;
  3. import java.time.ZoneId;
  4. import java.time.format.DateTimeFormatter;
  5. /**
  6. * 时间工具类。
  7. *
  8. * <p>提供当前时间、当前毫秒级时间戳、指定格式时间转时间戳、
  9. * 以及按天、月、年对时间戳进行加减计算的常用方法。</p>
  10. */
  11. public final class TimeUtil {
  12. /**
  13. * 默认时间格式:yyyy-MM-dd HH:mm:ss。
  14. *
  15. * <p>注意:Java 中 HH 表示 24 小时制,hh 表示 12 小时制。
  16. * 为了兼容常见输入,本工具类对外说明仍按 yyyy-MM-dd hh:mm:ss 理解,
  17. * 实际推荐传入 24 小时制时间,例如 2026-08-04 11:03:51。</p>
  18. */
  19. private static final String DEFAULT_PATTERN = "yyyy-MM-dd HH:mm:ss";
  20. /**
  21. * 默认系统时区。
  22. */
  23. private static final ZoneId DEFAULT_ZONE_ID = ZoneId.systemDefault();
  24. /**
  25. * 默认时间格式化器。
  26. */
  27. private static final DateTimeFormatter DEFAULT_FORMATTER = DateTimeFormatter.ofPattern(DEFAULT_PATTERN);
  28. private TimeUtil() {
  29. }
  30. /**
  31. * 获取当前时间字符串。
  32. *
  33. * @return 当前时间,格式为 yyyy-MM-dd HH:mm:ss
  34. */
  35. public static String getCurrentTime() {
  36. return LocalDateTime.now().format(DEFAULT_FORMATTER);
  37. }
  38. /**
  39. * 获取当前毫秒级时间戳。
  40. *
  41. * @return 当前毫秒级时间戳
  42. */
  43. public static long getCurrentTimestamp() {
  44. return System.currentTimeMillis();
  45. }
  46. /**
  47. * 将 yyyy-MM-dd HH:mm:ss 格式的时间字符串转换为毫秒级时间戳。
  48. *
  49. * @param dateTime 时间字符串,例如 2026-08-04 11:03:51
  50. * @return 对应的毫秒级时间戳
  51. */
  52. public static long toTimestamp(String dateTime) {
  53. if (dateTime == null || dateTime.trim().length() == 0) {
  54. throw new IllegalArgumentException("时间字符串不能为空");
  55. }
  56. LocalDateTime localDateTime = LocalDateTime.parse(dateTime.trim(), DEFAULT_FORMATTER);
  57. return localDateTime.atZone(DEFAULT_ZONE_ID).toInstant().toEpochMilli();
  58. }
  59. /**
  60. * 将毫秒级时间戳转换为 yyyy-MM-dd HH:mm:ss 格式的时间字符串。
  61. *
  62. * @param timestampMillis 毫秒级时间戳
  63. * @return 时间字符串
  64. */
  65. public static String toDateTime(long timestampMillis) {
  66. return LocalDateTime.ofInstant(java.time.Instant.ofEpochMilli(timestampMillis), DEFAULT_ZONE_ID)
  67. .format(DEFAULT_FORMATTER);
  68. }
  69. /**
  70. * 时间戳增加指定天数。
  71. *
  72. * @param timestampMillis 毫秒级时间戳
  73. * @param days 增加的天数
  74. * @return 计算后的毫秒级时间戳
  75. */
  76. public static long plusDays(long timestampMillis, long days) {
  77. return LocalDateTime.ofInstant(java.time.Instant.ofEpochMilli(timestampMillis), DEFAULT_ZONE_ID)
  78. .plusDays(days)
  79. .atZone(DEFAULT_ZONE_ID)
  80. .toInstant()
  81. .toEpochMilli();
  82. }
  83. /**
  84. * 时间戳减少指定天数。
  85. *
  86. * @param timestampMillis 毫秒级时间戳
  87. * @param days 减少的天数
  88. * @return 计算后的毫秒级时间戳
  89. */
  90. public static long minusDays(long timestampMillis, long days) {
  91. return plusDays(timestampMillis, -days);
  92. }
  93. /**
  94. * 时间戳增加指定月数。
  95. *
  96. * @param timestampMillis 毫秒级时间戳
  97. * @param months 增加的月数
  98. * @return 计算后的毫秒级时间戳
  99. */
  100. public static long plusMonths(long timestampMillis, long months) {
  101. return LocalDateTime.ofInstant(java.time.Instant.ofEpochMilli(timestampMillis), DEFAULT_ZONE_ID)
  102. .plusMonths(months)
  103. .atZone(DEFAULT_ZONE_ID)
  104. .toInstant()
  105. .toEpochMilli();
  106. }
  107. /**
  108. * 时间戳减少指定月数。
  109. *
  110. * @param timestampMillis 毫秒级时间戳
  111. * @param months 减少的月数
  112. * @return 计算后的毫秒级时间戳
  113. */
  114. public static long minusMonths(long timestampMillis, long months) {
  115. return plusMonths(timestampMillis, -months);
  116. }
  117. /**
  118. * 时间戳增加指定年数。
  119. *
  120. * @param timestampMillis 毫秒级时间戳
  121. * @param years 增加的年数
  122. * @return 计算后的毫秒级时间戳
  123. */
  124. public static long plusYears(long timestampMillis, long years) {
  125. return LocalDateTime.ofInstant(java.time.Instant.ofEpochMilli(timestampMillis), DEFAULT_ZONE_ID)
  126. .plusYears(years)
  127. .atZone(DEFAULT_ZONE_ID)
  128. .toInstant()
  129. .toEpochMilli();
  130. }
  131. /**
  132. * 时间戳减少指定年数。
  133. *
  134. * @param timestampMillis 毫秒级时间戳
  135. * @param years 减少的年数
  136. * @return 计算后的毫秒级时间戳
  137. */
  138. public static long minusYears(long timestampMillis, long years) {
  139. return plusYears(timestampMillis, -years);
  140. }
  141. }