DateUtils.java 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. package com.slibra.common.utils;
  2. import java.lang.management.ManagementFactory;
  3. import java.text.ParseException;
  4. import java.text.SimpleDateFormat;
  5. import java.time.LocalDate;
  6. import java.time.LocalDateTime;
  7. import java.time.LocalTime;
  8. import java.time.ZoneId;
  9. import java.time.ZonedDateTime;
  10. import java.time.format.DateTimeFormatter;
  11. import java.util.ArrayList;
  12. import java.util.Calendar;
  13. import java.util.Date;
  14. import java.util.List;
  15. import org.apache.commons.lang3.time.DateFormatUtils;
  16. /**
  17. * 时间工具类
  18. *
  19. *
  20. */
  21. public class DateUtils extends org.apache.commons.lang3.time.DateUtils
  22. {
  23. public static String YYYY = "yyyy";
  24. public static String YYYY_MM = "yyyy-MM";
  25. public static String YYYY_MM_DD = "yyyy-MM-dd";
  26. public static String YYYY_MM_DD_HH = "yyyy-MM-dd HH";
  27. public static String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
  28. public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
  29. public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
  30. public static String YYYYMMDDHHMM_TS = "yyyy/MM/dd HH:mm";
  31. public static String YYYYMMDDHH_TS = "yyyy/MM/dd HH";
  32. public static String YYYYMMDD_TS = "yyyy/MM/dd";
  33. public static String YYYYMM_TS = "yyyy/MM";
  34. public static String YYYYMMDDHH_CHINESE = "yyyy年MM月dd日 HH";
  35. private static String[] parsePatterns = {
  36. "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
  37. "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
  38. "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};
  39. /**
  40. * 获取当前Date型日期
  41. *
  42. * @return Date() 当前日期
  43. */
  44. public static Date getNowDate()
  45. {
  46. return new Date();
  47. }
  48. /**
  49. * 获取当前日期, 默认格式为yyyy-MM-dd
  50. *
  51. * @return String
  52. */
  53. public static String getDate()
  54. {
  55. return dateTimeNow(YYYY_MM_DD);
  56. }
  57. public static final String getTime()
  58. {
  59. return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
  60. }
  61. public static final String dateTimeNow()
  62. {
  63. return dateTimeNow(YYYYMMDDHHMMSS);
  64. }
  65. public static final String dateTimeNow(final String format)
  66. {
  67. return parseDateToStr(format, new Date());
  68. }
  69. public static final String dateTime(final Date date)
  70. {
  71. return parseDateToStr(YYYY_MM_DD, date);
  72. }
  73. public static final String parseDateToStr(final String format, final Date date)
  74. {
  75. return new SimpleDateFormat(format).format(date);
  76. }
  77. public static final Date dateTime(final String format, final String ts)
  78. {
  79. try
  80. {
  81. return new SimpleDateFormat(format).parse(ts);
  82. }
  83. catch (ParseException e)
  84. {
  85. throw new RuntimeException(e);
  86. }
  87. }
  88. /**
  89. * 日期路径 即年/月/日 如2018/08/08
  90. */
  91. public static final String datePath()
  92. {
  93. Date now = new Date();
  94. return DateFormatUtils.format(now, "yyyy/MM/dd");
  95. }
  96. /**
  97. * 日期路径 即年/月/日 如20180808
  98. */
  99. public static final String dateTime()
  100. {
  101. Date now = new Date();
  102. return DateFormatUtils.format(now, "yyyyMMdd");
  103. }
  104. /**
  105. * 日期型字符串转化为日期 格式
  106. */
  107. public static Date parseDate(Object str)
  108. {
  109. if (str == null)
  110. {
  111. return null;
  112. }
  113. try
  114. {
  115. return parseDate(str.toString(), parsePatterns);
  116. }
  117. catch (ParseException e)
  118. {
  119. return null;
  120. }
  121. }
  122. /**
  123. * 获取服务器启动时间
  124. */
  125. public static Date getServerStartDate()
  126. {
  127. long time = ManagementFactory.getRuntimeMXBean().getStartTime();
  128. return new Date(time);
  129. }
  130. /**
  131. * 计算相差天数
  132. */
  133. public static int differentDaysByMillisecond(Date date1, Date date2)
  134. {
  135. return Math.abs((int) ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24)));
  136. }
  137. /**
  138. * 计算相差小时数
  139. */
  140. public static int differentHoursByMillisecond(Date date1, Date date2)
  141. {
  142. return Math.abs((int) ((date2.getTime() - date1.getTime()) / (1000 * 3600)));
  143. }
  144. /**
  145. * 计算时间差
  146. *
  147. * @param endDate 最后时间
  148. * @param startTime 开始时间
  149. * @return 时间差(天/小时/分钟)
  150. */
  151. public static String timeDistance(Date endDate, Date startTime)
  152. {
  153. long nd = 1000 * 24 * 60 * 60;
  154. long nh = 1000 * 60 * 60;
  155. long nm = 1000 * 60;
  156. // long ns = 1000;
  157. // 获得两个时间的毫秒时间差异
  158. long diff = endDate.getTime() - startTime.getTime();
  159. // 计算差多少天
  160. long day = diff / nd;
  161. // 计算差多少小时
  162. long hour = diff % nd / nh;
  163. // 计算差多少分钟
  164. long min = diff % nd % nh / nm;
  165. // 计算差多少秒//输出结果
  166. // long sec = diff % nd % nh % nm / ns;
  167. return day + "天" + hour + "小时" + min + "分钟";
  168. }
  169. /**
  170. * 增加 LocalDateTime ==> Date
  171. */
  172. public static Date toDate(LocalDateTime temporalAccessor)
  173. {
  174. ZonedDateTime zdt = temporalAccessor.atZone(ZoneId.systemDefault());
  175. return Date.from(zdt.toInstant());
  176. }
  177. /**
  178. * 增加 LocalDate ==> Date
  179. */
  180. public static Date toDate(LocalDate temporalAccessor)
  181. {
  182. LocalDateTime localDateTime = LocalDateTime.of(temporalAccessor, LocalTime.of(0, 0, 0));
  183. ZonedDateTime zdt = localDateTime.atZone(ZoneId.systemDefault());
  184. return Date.from(zdt.toInstant());
  185. }
  186. public static Date plusDate(int day, Date current)
  187. {
  188. // 创建一个Calendar实例,并将日期设置为今天
  189. Calendar calendar = Calendar.getInstance();
  190. calendar.setTime(current);
  191. // 将日期设置为前一天
  192. calendar.add(Calendar.DATE, day);
  193. // 转换
  194. return calendar.getTime();
  195. }
  196. public static Date plusHour(int hour, Date current)
  197. {
  198. // 创建一个Calendar实例,并将日期设置为今天
  199. Calendar calendar = Calendar.getInstance();
  200. calendar.setTime(current);
  201. // 将日期设置为前一天
  202. calendar.add(Calendar.HOUR, hour);
  203. // 转换
  204. return calendar.getTime();
  205. }
  206. public static List<String> getAllDatesBetween(LocalDate startDate, LocalDate endDate) {
  207. List<String> dates = new ArrayList<>();
  208. LocalDate currentDate = startDate;
  209. while (!currentDate.isAfter(endDate)) {
  210. dates.add(currentDate.format(DateTimeFormatter.ofPattern(DateUtils.YYYYMMDD_TS)));
  211. currentDate = currentDate.plusDays(1);
  212. }
  213. return dates;
  214. }
  215. public static String getMonthsBefore(String inputDate, int n) {
  216. DateTimeFormatter formatter = DateTimeFormatter.ofPattern(YYYY_MM_DD);
  217. DateTimeFormatter formatterOut = DateTimeFormatter.ofPattern(YYYY_MM);
  218. LocalDate date = LocalDate.parse(inputDate + "-01", formatter.withZone(java.time.ZoneId.systemDefault()));
  219. // 获取 n 个月之前的日期
  220. LocalDate targetDate = date.minusMonths(n);
  221. // 格式化目标日期为 "yyyy-MM" 格式
  222. return targetDate.format(formatterOut);
  223. }
  224. public static void main(String[] args) {
  225. /*Date nowDate = DateUtils.getNowDate();
  226. Date date = plusDate(1, nowDate);
  227. Date date1 = plusDate(-1, nowDate);
  228. System.out.println(date);
  229. System.out.println(date1);
  230. System.out.println(getMonthsBefore("2024-02", 2));*/
  231. System.out.println(plusHour(-1, getNowDate()));
  232. }
  233. }