DateUtils.java 7.1 KB

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