DateUtils.java 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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.*;
  6. import java.util.Calendar;
  7. import java.util.Date;
  8. import org.apache.commons.lang3.time.DateFormatUtils;
  9. /**
  10. * 时间工具类
  11. *
  12. * @author ruoyi
  13. */
  14. public class DateUtils extends org.apache.commons.lang3.time.DateUtils
  15. {
  16. public static String YYYY = "yyyy";
  17. public static String YYYY_MM = "yyyy-MM";
  18. public static String YYYY_MM_DD = "yyyy-MM-dd";
  19. public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
  20. public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
  21. private static String[] parsePatterns = {
  22. "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
  23. "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
  24. "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};
  25. /**
  26. * 获取当前Date型日期
  27. *
  28. * @return Date() 当前日期
  29. */
  30. public static Date getNowDate()
  31. {
  32. return new Date();
  33. }
  34. /**
  35. * 获取当前日期, 默认格式为yyyy-MM-dd
  36. *
  37. * @return String
  38. */
  39. public static String getDate()
  40. {
  41. return dateTimeNow(YYYY_MM_DD);
  42. }
  43. public static String getYear()
  44. {
  45. return dateTimeNow(YYYY);
  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 endDate 最后时间
  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. /**
  170. * 增加 Date ==> LocalDate
  171. */
  172. public static LocalDate toLocalDate(Date date)
  173. {
  174. // 转换为java.time.LocalDate
  175. LocalDate localDate = date.toInstant()
  176. .atZone(ZoneId.systemDefault())
  177. .toLocalDate();
  178. return localDate;
  179. }
  180. public static int getWeekOfMonth(Date date)
  181. {
  182. Calendar calendar = Calendar.getInstance();
  183. // 设置每周的第一天是周一(Calendar.MONDAY = 2)
  184. calendar.setFirstDayOfWeek(Calendar.MONDAY);
  185. // 设置一周的最小天数为1天(不足一周则补至下周)
  186. calendar.setMinimalDaysInFirstWeek(1);
  187. calendar.setTime(date);
  188. int weekOfMonth = calendar.get(Calendar.WEEK_OF_MONTH);
  189. return weekOfMonth;
  190. }
  191. /**
  192. * 获取时间戳
  193. *
  194. * @return
  195. */
  196. public static String getUnix() {
  197. return Long.toString(System.currentTimeMillis() / 1000L);
  198. }
  199. /**
  200. * 返回 年月日小时分秒 毫秒
  201. *
  202. * @return yyyyMMddHHmmssS
  203. */
  204. public static String getTodayChar() {
  205. String dateString = DateFormatUtils.format(new Date(), "yyyyMMddHHmmssS");
  206. int length = dateString.length();
  207. if (length < 17) {
  208. StringBuilder endStr = new StringBuilder(dateString.substring(14, length));
  209. int len = endStr.length();
  210. for (int i = 0; i < 3 - len; i++) {
  211. endStr.insert(0, "0");
  212. }
  213. dateString = dateString.substring(0, 14) + endStr;
  214. }
  215. return dateString;
  216. }
  217. /**
  218. * 判断一个字字符串能不能转成LONG
  219. *
  220. * @return yyyyMMddHHmmssS
  221. */
  222. public static boolean isNumeric(String strNum) {
  223. try {
  224. // 尝试将字符串转换为数字
  225. Long num = Long.parseLong(strNum);
  226. // 是数字,返回true
  227. return true;
  228. } catch (NumberFormatException e) {
  229. // 不是数字,捕获异常并返回false
  230. return false;
  231. }
  232. }
  233. /**
  234. * 获取当前月
  235. *
  236. * @return
  237. */
  238. public static String getMonth()
  239. {
  240. LocalDate currentDate = LocalDate.now();
  241. int currentMonth = currentDate.getMonthValue();
  242. return String.valueOf(currentMonth);
  243. }
  244. /**
  245. * 获取当前日
  246. *
  247. * @return
  248. */
  249. public static String getDay()
  250. {
  251. LocalDate now = LocalDate.now();
  252. int day = now.getDayOfMonth();
  253. return String.valueOf(day);
  254. }
  255. /**
  256. * 两个日期之间相差的月数
  257. * @param date1 开始时间
  258. * @param date2 结束时间
  259. * @return
  260. */
  261. public static int calculateMonths(String date1, String date2) {
  262. LocalDate localDate1 = LocalDate.parse(date1);
  263. LocalDate localDate2 = LocalDate.parse(date2);
  264. Period period = Period.between(localDate1, localDate2);
  265. return period.getMonths();
  266. }
  267. }