DateUtils.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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.util.Date;
  11. import org.apache.commons.lang3.time.DateFormatUtils;
  12. /**
  13. * 时间工具类
  14. *
  15. *
  16. */
  17. public class DateUtils extends org.apache.commons.lang3.time.DateUtils
  18. {
  19. public static String YYYY = "yyyy";
  20. public static String YYYY_MM = "yyyy-MM";
  21. public static String YYYY_MM_DD = "yyyy-MM-dd";
  22. public static String YYYY_MM_DD_HH = "yyyy-MM-dd HH";
  23. public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
  24. public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
  25. public static String YYYYMMDDHHMM_TS = "yyyy/MM/dd HH:mm";
  26. public static String YYYYMMDDHH_TS = "yyyy/MM/dd HH";
  27. public static String YYYYMMDD_TS = "yyyy/MM/dd";
  28. public static String YYYYMMDDHH_CHINESE = "yyyy年MM月dd日 HH";
  29. private static String[] parsePatterns = {
  30. "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
  31. "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
  32. "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};
  33. /**
  34. * 获取当前Date型日期
  35. *
  36. * @return Date() 当前日期
  37. */
  38. public static Date getNowDate()
  39. {
  40. return new Date();
  41. }
  42. /**
  43. * 获取当前日期, 默认格式为yyyy-MM-dd
  44. *
  45. * @return String
  46. */
  47. public static String getDate()
  48. {
  49. return dateTimeNow(YYYY_MM_DD);
  50. }
  51. public static final String getTime()
  52. {
  53. return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
  54. }
  55. public static final String dateTimeNow()
  56. {
  57. return dateTimeNow(YYYYMMDDHHMMSS);
  58. }
  59. public static final String dateTimeNow(final String format)
  60. {
  61. return parseDateToStr(format, new Date());
  62. }
  63. public static final String dateTime(final Date date)
  64. {
  65. return parseDateToStr(YYYY_MM_DD, date);
  66. }
  67. public static final String parseDateToStr(final String format, final Date date)
  68. {
  69. return new SimpleDateFormat(format).format(date);
  70. }
  71. public static final Date dateTime(final String format, final String ts)
  72. {
  73. try
  74. {
  75. return new SimpleDateFormat(format).parse(ts);
  76. }
  77. catch (ParseException e)
  78. {
  79. throw new RuntimeException(e);
  80. }
  81. }
  82. /**
  83. * 日期路径 即年/月/日 如2018/08/08
  84. */
  85. public static final String datePath()
  86. {
  87. Date now = new Date();
  88. return DateFormatUtils.format(now, "yyyy/MM/dd");
  89. }
  90. /**
  91. * 日期路径 即年/月/日 如20180808
  92. */
  93. public static final String dateTime()
  94. {
  95. Date now = new Date();
  96. return DateFormatUtils.format(now, "yyyyMMdd");
  97. }
  98. /**
  99. * 日期型字符串转化为日期 格式
  100. */
  101. public static Date parseDate(Object str)
  102. {
  103. if (str == null)
  104. {
  105. return null;
  106. }
  107. try
  108. {
  109. return parseDate(str.toString(), parsePatterns);
  110. }
  111. catch (ParseException e)
  112. {
  113. return null;
  114. }
  115. }
  116. /**
  117. * 获取服务器启动时间
  118. */
  119. public static Date getServerStartDate()
  120. {
  121. long time = ManagementFactory.getRuntimeMXBean().getStartTime();
  122. return new Date(time);
  123. }
  124. /**
  125. * 计算相差天数
  126. */
  127. public static int differentDaysByMillisecond(Date date1, Date date2)
  128. {
  129. return Math.abs((int) ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24)));
  130. }
  131. /**
  132. * 计算时间差
  133. *
  134. * @param endDate 最后时间
  135. * @param startTime 开始时间
  136. * @return 时间差(天/小时/分钟)
  137. */
  138. public static String timeDistance(Date endDate, Date startTime)
  139. {
  140. long nd = 1000 * 24 * 60 * 60;
  141. long nh = 1000 * 60 * 60;
  142. long nm = 1000 * 60;
  143. // long ns = 1000;
  144. // 获得两个时间的毫秒时间差异
  145. long diff = endDate.getTime() - startTime.getTime();
  146. // 计算差多少天
  147. long day = diff / nd;
  148. // 计算差多少小时
  149. long hour = diff % nd / nh;
  150. // 计算差多少分钟
  151. long min = diff % nd % nh / nm;
  152. // 计算差多少秒//输出结果
  153. // long sec = diff % nd % nh % nm / ns;
  154. return day + "天" + hour + "小时" + min + "分钟";
  155. }
  156. /**
  157. * 增加 LocalDateTime ==> Date
  158. */
  159. public static Date toDate(LocalDateTime temporalAccessor)
  160. {
  161. ZonedDateTime zdt = temporalAccessor.atZone(ZoneId.systemDefault());
  162. return Date.from(zdt.toInstant());
  163. }
  164. /**
  165. * 增加 LocalDate ==> Date
  166. */
  167. public static Date toDate(LocalDate temporalAccessor)
  168. {
  169. LocalDateTime localDateTime = LocalDateTime.of(temporalAccessor, LocalTime.of(0, 0, 0));
  170. ZonedDateTime zdt = localDateTime.atZone(ZoneId.systemDefault());
  171. return Date.from(zdt.toInstant());
  172. }
  173. }