DateUtils.java 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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.time.format.DateTimeFormatter;
  11. import java.util.Date;
  12. import java.util.concurrent.ThreadLocalRandom;
  13. import org.apache.commons.lang3.time.DateFormatUtils;
  14. /**
  15. * 时间工具类
  16. *
  17. * @author ruoyi
  18. */
  19. public class DateUtils extends org.apache.commons.lang3.time.DateUtils
  20. {
  21. public static String YYYY = "yyyy";
  22. public static String YYYY_MM = "yyyy-MM";
  23. public static String YYYY_MM_DD = "yyyy-MM-dd";
  24. public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
  25. public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
  26. private static String[] parsePatterns = {
  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. "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};
  30. /**
  31. * 获取当前Date型日期
  32. *
  33. * @return Date() 当前日期
  34. */
  35. public static Date getNowDate()
  36. {
  37. return new Date();
  38. }
  39. /**
  40. * 获取当前日期, 默认格式为yyyy-MM-dd
  41. *
  42. * @return String
  43. */
  44. public static String getDate()
  45. {
  46. return dateTimeNow(YYYY_MM_DD);
  47. }
  48. public static final String getTime()
  49. {
  50. return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
  51. }
  52. public static final String dateTimeNow()
  53. {
  54. return dateTimeNow(YYYYMMDDHHMMSS);
  55. }
  56. public static final String dateTimeNow(final String format)
  57. {
  58. return parseDateToStr(format, new Date());
  59. }
  60. public static final String dateTime(final Date date)
  61. {
  62. return parseDateToStr(YYYY_MM_DD, date);
  63. }
  64. public static final String parseDateToStr(final String format, final Date date)
  65. {
  66. return new SimpleDateFormat(format).format(date);
  67. }
  68. public static final Date dateTime(final String format, final String ts)
  69. {
  70. try
  71. {
  72. return new SimpleDateFormat(format).parse(ts);
  73. }
  74. catch (ParseException e)
  75. {
  76. throw new RuntimeException(e);
  77. }
  78. }
  79. /**
  80. * 日期路径 即年/月/日 如2018/08/08
  81. */
  82. public static final String datePath()
  83. {
  84. Date now = new Date();
  85. return DateFormatUtils.format(now, "yyyy/MM/dd");
  86. }
  87. /**
  88. * 日期路径 即年/月/日 如20180808
  89. */
  90. public static final String dateTime()
  91. {
  92. Date now = new Date();
  93. return DateFormatUtils.format(now, "yyyyMMdd");
  94. }
  95. /**
  96. * 日期型字符串转化为日期 格式
  97. */
  98. public static Date parseDate(Object str)
  99. {
  100. if (str == null)
  101. {
  102. return null;
  103. }
  104. try
  105. {
  106. return parseDate(str.toString(), parsePatterns);
  107. }
  108. catch (ParseException e)
  109. {
  110. return null;
  111. }
  112. }
  113. /**
  114. * 获取服务器启动时间
  115. */
  116. public static Date getServerStartDate()
  117. {
  118. long time = ManagementFactory.getRuntimeMXBean().getStartTime();
  119. return new Date(time);
  120. }
  121. /**
  122. * 计算相差天数
  123. */
  124. public static int differentDaysByMillisecond(Date date1, Date date2)
  125. {
  126. return Math.abs((int) ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24)));
  127. }
  128. /**
  129. * 计算时间差
  130. *
  131. * @param endDate 最后时间
  132. * @param startTime 开始时间
  133. * @return 时间差(天/小时/分钟)
  134. */
  135. public static String timeDistance(Date endDate, Date startTime)
  136. {
  137. long nd = 1000 * 24 * 60 * 60;
  138. long nh = 1000 * 60 * 60;
  139. long nm = 1000 * 60;
  140. // long ns = 1000;
  141. // 获得两个时间的毫秒时间差异
  142. long diff = endDate.getTime() - startTime.getTime();
  143. // 计算差多少天
  144. long day = diff / nd;
  145. // 计算差多少小时
  146. long hour = diff % nd / nh;
  147. // 计算差多少分钟
  148. long min = diff % nd % nh / nm;
  149. // 计算差多少秒//输出结果
  150. // long sec = diff % nd % nh % nm / ns;
  151. return day + "天" + hour + "小时" + min + "分钟";
  152. }
  153. /**
  154. * 增加 LocalDateTime ==> Date
  155. */
  156. public static Date toDate(LocalDateTime temporalAccessor)
  157. {
  158. ZonedDateTime zdt = temporalAccessor.atZone(ZoneId.systemDefault());
  159. return Date.from(zdt.toInstant());
  160. }
  161. /**
  162. * 增加 LocalDate ==> Date
  163. */
  164. public static Date toDate(LocalDate temporalAccessor)
  165. {
  166. LocalDateTime localDateTime = LocalDateTime.of(temporalAccessor, LocalTime.of(0, 0, 0));
  167. ZonedDateTime zdt = localDateTime.atZone(ZoneId.systemDefault());
  168. return Date.from(zdt.toInstant());
  169. }
  170. /**
  171. * 时间戳+4位随机数
  172. */
  173. public static final String getOrderId()
  174. {
  175. // 获取当前时间戳
  176. long timestamp = System.currentTimeMillis();
  177. // 使用ThreadLocalRandom生成一个0到9999之间的随机数(包括0和9999)
  178. int randomNumber = ThreadLocalRandom.current().nextInt(10000);
  179. // 拼接时间戳和随机数,并确保随机数是4位数
  180. String timestampWithRandom = timestamp + String.format("%04d", randomNumber);
  181. return timestampWithRandom;
  182. }
  183. /**
  184. * 获取上个月(年月)
  185. */
  186. public static final String lastMonth()
  187. {
  188. LocalDate currentDate = LocalDate.now();
  189. LocalDate lastMonthDate = currentDate.minusMonths(1);
  190. DateTimeFormatter formatter = DateTimeFormatter.ofPattern(YYYY_MM);
  191. String lastMonth = lastMonthDate.format(formatter);
  192. return lastMonth;
  193. }
  194. /**
  195. * 获取当前季度的开始日期
  196. */
  197. public static final String startQuarterly()
  198. {
  199. // 获取当前日期
  200. LocalDate currentDate = LocalDate.now();
  201. // 计算当前季度
  202. int month = currentDate.getMonthValue();
  203. int startMonth, endMonth;
  204. if (month >= 1 && month <= 3) {
  205. startMonth = 1;
  206. endMonth = 3;
  207. } else if (month >= 4 && month <= 6) {
  208. startMonth = 4;
  209. endMonth = 6;
  210. } else if (month >= 7 && month <= 9) {
  211. startMonth = 7;
  212. endMonth = 9;
  213. } else {
  214. startMonth = 10;
  215. endMonth = 12;
  216. }
  217. // 构建开始和结束日期
  218. LocalDate startDate = LocalDate.of(currentDate.getYear(), startMonth, 1);
  219. LocalDate endDate = LocalDate.of(currentDate.getYear(), endMonth,
  220. endMonth == 6 || endMonth == 9 ? 30 : 31);
  221. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
  222. return startDate.format(formatter);
  223. }
  224. /**
  225. * 获取当前季度的结束日期
  226. */
  227. public static final String endQuarterly()
  228. {
  229. // 获取当前日期
  230. LocalDate currentDate = LocalDate.now();
  231. // 计算当前季度
  232. int month = currentDate.getMonthValue();
  233. int startMonth, endMonth;
  234. if (month >= 1 && month <= 3) {
  235. startMonth = 1;
  236. endMonth = 3;
  237. } else if (month >= 4 && month <= 6) {
  238. startMonth = 4;
  239. endMonth = 6;
  240. } else if (month >= 7 && month <= 9) {
  241. startMonth = 7;
  242. endMonth = 9;
  243. } else {
  244. startMonth = 10;
  245. endMonth = 12;
  246. }
  247. // 构建开始和结束日期
  248. LocalDate startDate = LocalDate.of(currentDate.getYear(), startMonth, 1);
  249. LocalDate endDate = LocalDate.of(currentDate.getYear(), endMonth,
  250. endMonth == 6 || endMonth == 9 ? 30 : 31);
  251. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
  252. return endDate.format(formatter);
  253. }
  254. }