DateUtils.java 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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.Calendar;
  11. import java.util.Date;
  12. import java.util.GregorianCalendar;
  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. 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. return new Date();
  36. }
  37. /**
  38. * 获取当前日期, 默认格式为yyyy-MM-dd
  39. *
  40. * @return String
  41. */
  42. public static String getDate() {
  43. return dateTimeNow(YYYY_MM_DD);
  44. }
  45. public static final String getTime() {
  46. return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
  47. }
  48. public static final String dateTimeNow() {
  49. return dateTimeNow(YYYYMMDDHHMMSS);
  50. }
  51. public static final String dateTimeNow(final String format) {
  52. return parseDateToStr(format, new Date());
  53. }
  54. public static final String dateTime(final Date date) {
  55. return parseDateToStr(YYYY_MM_DD, date);
  56. }
  57. public static final String parseDateToStr(final String format, final Date date) {
  58. return new SimpleDateFormat(format).format(date);
  59. }
  60. public static final Date dateTime(final String format, final String ts) {
  61. try {
  62. return new SimpleDateFormat(format).parse(ts);
  63. } catch (ParseException e) {
  64. throw new RuntimeException(e);
  65. }
  66. }
  67. /**
  68. * 日期路径 即年/月/日 如2018/08/08
  69. */
  70. public static final String datePath() {
  71. Date now = new Date();
  72. return DateFormatUtils.format(now, "yyyy/MM/dd");
  73. }
  74. /**
  75. * 日期路径 即年/月/日 如20180808
  76. */
  77. public static final String dateTime() {
  78. Date now = new Date();
  79. return DateFormatUtils.format(now, "yyyyMMdd");
  80. }
  81. /**
  82. * 日期型字符串转化为日期 格式
  83. */
  84. public static Date parseDate(Object str) {
  85. if (str == null) {
  86. return null;
  87. }
  88. try {
  89. return parseDate(str.toString(), parsePatterns);
  90. } catch (ParseException e) {
  91. return null;
  92. }
  93. }
  94. /**
  95. * 获取服务器启动时间
  96. */
  97. public static Date getServerStartDate() {
  98. long time = ManagementFactory.getRuntimeMXBean().getStartTime();
  99. return new Date(time);
  100. }
  101. /**
  102. * 计算相差天数
  103. */
  104. public static int differentDaysByMillisecond(Date date1, Date date2) {
  105. return Math.abs((int) ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24)));
  106. }
  107. /**
  108. * 计算两个时间差(小时)
  109. * sendDate 结束时间
  110. * snowDate 开始时间
  111. *
  112. */
  113. public static Long getDateHour(String sendDate, String snowDate) {
  114. SimpleDateFormat simpleDateFormat = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
  115. try {
  116. Date nowDate = simpleDateFormat.parse(snowDate);
  117. Date endDate = simpleDateFormat.parse(sendDate);
  118. long nd = 1000 * 24 * 60 * 60;
  119. long nh = 1000 * 60 * 60;
  120. long nm = 1000 * 60;
  121. // long ns = 1000;
  122. // 获得两个时间的毫秒时间差异
  123. long diff = endDate.getTime() - nowDate.getTime();
  124. // 计算差多少天
  125. long day = diff / nd;
  126. // 计算差多少小时
  127. long hour = diff % nd / nh;
  128. Long hours = day*24+hour;
  129. return hours;
  130. } catch (ParseException e) {
  131. e.printStackTrace();
  132. }
  133. return null;
  134. }
  135. /**
  136. * 计算两个时间差
  137. */
  138. public static String getDatePoor(Date endDate, Date nowDate) {
  139. long nd = 1000 * 24 * 60 * 60;
  140. long nh = 1000 * 60 * 60;
  141. long nm = 1000 * 60;
  142. // long ns = 1000;
  143. // 获得两个时间的毫秒时间差异
  144. long diff = endDate.getTime() - nowDate.getTime();
  145. // 计算差多少天
  146. long day = diff / nd;
  147. // 计算差多少小时
  148. long hour = diff % nd / nh;
  149. // 计算差多少分钟
  150. long min = diff % nd % nh / nm;
  151. // 计算差多少秒//输出结果
  152. // long sec = diff % nd % nh % nm / ns;
  153. return day + "天" + hour + "小时" + min + "分钟";
  154. }
  155. /**
  156. * 增加 LocalDateTime ==> Date
  157. */
  158. public static Date toDate(LocalDateTime temporalAccessor) {
  159. ZonedDateTime zdt = temporalAccessor.atZone(ZoneId.systemDefault());
  160. return Date.from(zdt.toInstant());
  161. }
  162. /**
  163. * 增加 LocalDate ==> Date
  164. */
  165. public static Date toDate(LocalDate temporalAccessor) {
  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. * 获取本周一的日期
  172. */
  173. public static String getCurrentMonday() {
  174. SimpleDateFormat dateFormat = new SimpleDateFormat(YYYY_MM_DD);
  175. int mondayPlus = getMondayPlus();
  176. GregorianCalendar currentDate = new GregorianCalendar();
  177. currentDate.add(GregorianCalendar.DATE, mondayPlus);
  178. Date monday = currentDate.getTime();
  179. return dateFormat.format(monday);
  180. }
  181. /**
  182. * 获取当前日期与本周一相差的天数
  183. */
  184. private static int getMondayPlus() {
  185. Calendar cd = Calendar.getInstance();
  186. // 默认:星期日是第一天
  187. int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK);
  188. if (dayOfWeek == 1) {
  189. return -6;
  190. } else {
  191. return 2 - dayOfWeek;
  192. }
  193. }
  194. /**
  195. * 获取给定日期N天后的日期
  196. *
  197. * @param dateTime
  198. * @param days
  199. * @return
  200. */
  201. public static String getDateAfterNDays(String dateTime, int days) {
  202. Calendar calendar = Calendar.getInstance();
  203. String[] dateTimeArray = dateTime.split("-");
  204. int year = Integer.parseInt(dateTimeArray[0]);
  205. int month = Integer.parseInt(dateTimeArray[1]);
  206. int day = Integer.parseInt(dateTimeArray[2]);
  207. calendar.set(year, month - 1, day);
  208. long time = calendar.getTimeInMillis();// 给定时间与1970 年 1 月 1 日的00:00:00.000的差。以毫秒显示
  209. calendar.setTimeInMillis(time + days * 1000 * 60 * 60 * 24);// 用给定的 long值设置此Calendar的当前时间值
  210. return calendar.get(Calendar.YEAR)// 年
  211. + "-" + (calendar.get(Calendar.MONTH) + 1)// 月
  212. + "-" + calendar.get(Calendar.DAY_OF_MONTH)// 日
  213. ;
  214. }
  215. public static String getAddDate(String dateTime,int n) {
  216. SimpleDateFormat simpleDateFormat = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
  217. Date holdDate = null;
  218. try {
  219. holdDate = simpleDateFormat.parse(dateTime);
  220. Calendar calendar =new GregorianCalendar();
  221. calendar.setTime(holdDate);
  222. calendar.add(calendar.DATE, n);
  223. Date utilDate = calendar.getTime();
  224. holdDate =new Date(utilDate.getTime());
  225. String rDate = simpleDateFormat.format(holdDate);
  226. return rDate;
  227. } catch (ParseException e) {
  228. e.printStackTrace();
  229. }
  230. return null;
  231. }
  232. }