DateUtils.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. package com.ruoyi.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 java.util.regex.Pattern;
  12. import org.apache.commons.lang3.time.DateFormatUtils;
  13. /**
  14. * 时间工具类
  15. *
  16. * @author ruoyi
  17. */
  18. public class DateUtils extends org.apache.commons.lang3.time.DateUtils
  19. {
  20. public static String YYYY = "yyyy";
  21. public static String YYYY_MM = "yyyy-MM";
  22. public static String YYYY_MM_DD = "yyyy-MM-dd";
  23. public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
  24. public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
  25. private static String[] parsePatterns = {
  26. "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
  27. "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
  28. "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};
  29. /**
  30. * 获取当前Date型日期
  31. *
  32. * @return Date() 当前日期
  33. */
  34. public static Date getNowDate()
  35. {
  36. return new Date();
  37. }
  38. /**
  39. * 获取当前日期, 默认格式为yyyy-MM-dd
  40. *
  41. * @return String
  42. */
  43. public static String getDate()
  44. {
  45. return dateTimeNow(YYYY_MM_DD);
  46. }
  47. public static final String getTime()
  48. {
  49. return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
  50. }
  51. public static final String dateTimeNow()
  52. {
  53. return dateTimeNow(YYYYMMDDHHMMSS);
  54. }
  55. public static final String dateTimeNow(final String format)
  56. {
  57. return parseDateToStr(format, new Date());
  58. }
  59. public static final String dateTime(final Date date)
  60. {
  61. return parseDateToStr(YYYY_MM_DD, date);
  62. }
  63. public static final String parseDateToStr(final String format, final Date date)
  64. {
  65. return new SimpleDateFormat(format).format(date);
  66. }
  67. public static final Date dateTime(final String format, final String ts)
  68. {
  69. try
  70. {
  71. return new SimpleDateFormat(format).parse(ts);
  72. }
  73. catch (ParseException e)
  74. {
  75. throw new RuntimeException(e);
  76. }
  77. }
  78. /**
  79. * 日期路径 即年/月/日 如2018/08/08
  80. */
  81. public static final String datePath()
  82. {
  83. Date now = new Date();
  84. return DateFormatUtils.format(now, "yyyy/MM/dd");
  85. }
  86. /**
  87. * 日期路径 即年/月/日 如20180808
  88. */
  89. public static final String dateTime()
  90. {
  91. Date now = new Date();
  92. return DateFormatUtils.format(now, "yyyyMMdd");
  93. }
  94. /**
  95. * 日期型字符串转化为日期 格式
  96. */
  97. public static Date parseDate(Object str)
  98. {
  99. if (str == null)
  100. {
  101. return null;
  102. }
  103. try
  104. {
  105. return parseDate(str.toString(), parsePatterns);
  106. }
  107. catch (ParseException e)
  108. {
  109. return null;
  110. }
  111. }
  112. /**
  113. * 获取服务器启动时间
  114. */
  115. public static Date getServerStartDate()
  116. {
  117. long time = ManagementFactory.getRuntimeMXBean().getStartTime();
  118. return new Date(time);
  119. }
  120. /**
  121. * 计算相差天数
  122. */
  123. public static int differentDaysByMillisecond(Date date1, Date date2)
  124. {
  125. return Math.abs((int) ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24)));
  126. }
  127. /**
  128. * 计算时间差
  129. *
  130. * @param endTime 最后时间
  131. * @param startTime 开始时间
  132. * @return 时间差(天/小时/分钟)
  133. */
  134. public static String timeDistance(Date endDate, Date startTime)
  135. {
  136. long nd = 1000 * 24 * 60 * 60;
  137. long nh = 1000 * 60 * 60;
  138. long nm = 1000 * 60;
  139. // long ns = 1000;
  140. // 获得两个时间的毫秒时间差异
  141. long diff = endDate.getTime() - startTime.getTime();
  142. // 计算差多少天
  143. long day = diff / nd;
  144. // 计算差多少小时
  145. long hour = diff % nd / nh;
  146. // 计算差多少分钟
  147. long min = diff % nd % nh / nm;
  148. // 计算差多少秒//输出结果
  149. // long sec = diff % nd % nh % nm / ns;
  150. return day + "天" + hour + "小时" + min + "分钟";
  151. }
  152. /**
  153. * 增加 LocalDateTime ==> Date
  154. */
  155. public static Date toDate(LocalDateTime temporalAccessor)
  156. {
  157. ZonedDateTime zdt = temporalAccessor.atZone(ZoneId.systemDefault());
  158. return Date.from(zdt.toInstant());
  159. }
  160. /**
  161. * 增加 LocalDate ==> Date
  162. */
  163. public static Date toDate(LocalDate temporalAccessor)
  164. {
  165. LocalDateTime localDateTime = LocalDateTime.of(temporalAccessor, LocalTime.of(0, 0, 0));
  166. ZonedDateTime zdt = localDateTime.atZone(ZoneId.systemDefault());
  167. return Date.from(zdt.toInstant());
  168. }
  169. public static String getDateFormat(String str) {
  170. boolean year = false;
  171. Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
  172. if (pattern.matcher(str.substring(0, 4)).matches()) {
  173. year = true;
  174. }
  175. StringBuilder sb = new StringBuilder();
  176. int index = 0;
  177. if (!year) {
  178. if (str.contains("月") || str.contains("-") || str.contains("/") || str.contains(".")) {
  179. if (Character.isDigit(str.charAt(0))) {
  180. index = 1;
  181. }
  182. } else {
  183. index = 3;
  184. }
  185. }
  186. for (int i = 0; i < str.length(); i++) {
  187. char chr = str.charAt(i);
  188. if (Character.isDigit(chr)) {
  189. if (index == 0) {
  190. sb.append("y");
  191. }
  192. if (index == 1) {
  193. sb.append("M");
  194. }
  195. if (index == 2) {
  196. sb.append("d");
  197. }
  198. if (index == 3) {
  199. sb.append("H");
  200. }
  201. if (index == 4) {
  202. sb.append("m");
  203. }
  204. if (index == 5) {
  205. sb.append("s");
  206. }
  207. if (index == 6) {
  208. sb.append("S");
  209. }
  210. } else {
  211. if (i > 0) {
  212. char lastChar = str.charAt(i - 1);
  213. if (Character.isDigit(lastChar)) {
  214. index++;
  215. }
  216. }
  217. sb.append(chr);
  218. }
  219. }
  220. return sb.toString();
  221. }
  222. }