DateUtils.java 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. package com.ruoyi.common.utils;
  2. import java.lang.management.ManagementFactory;
  3. import java.text.DateFormat;
  4. import java.text.ParseException;
  5. import java.text.SimpleDateFormat;
  6. import java.util.Date;
  7. import java.util.regex.Pattern;
  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 final String getTime()
  44. {
  45. return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
  46. }
  47. public static final String dateTimeNow()
  48. {
  49. return dateTimeNow(YYYYMMDDHHMMSS);
  50. }
  51. public static final String dateTimeNow(final String format)
  52. {
  53. return parseDateToStr(format, new Date());
  54. }
  55. public static final String dateTime(final Date date)
  56. {
  57. return parseDateToStr(YYYY_MM_DD, date);
  58. }
  59. public static final String parseDateToStr(final String format, final Date date)
  60. {
  61. return new SimpleDateFormat(format).format(date);
  62. }
  63. public static final Date dateTime(final String format, final String ts)
  64. {
  65. try
  66. {
  67. return new SimpleDateFormat(format).parse(ts);
  68. }
  69. catch (ParseException e)
  70. {
  71. throw new RuntimeException(e);
  72. }
  73. }
  74. /**
  75. * 日期路径 即年/月/日 如2018/08/08
  76. */
  77. public static final String datePath()
  78. {
  79. Date now = new Date();
  80. return DateFormatUtils.format(now, "yyyy/MM/dd");
  81. }
  82. /**
  83. * 日期路径 即年/月/日 如20180808
  84. */
  85. public static final String dateTime()
  86. {
  87. Date now = new Date();
  88. return DateFormatUtils.format(now, "yyyyMMdd");
  89. }
  90. /**
  91. * 日期型字符串转化为日期 格式
  92. */
  93. public static Date parseDate(Object str)
  94. {
  95. if (str == null)
  96. {
  97. return null;
  98. }
  99. try
  100. {
  101. return parseDate(str.toString(), parsePatterns);
  102. }
  103. catch (ParseException e)
  104. {
  105. return null;
  106. }
  107. }
  108. /**
  109. * 获取服务器启动时间
  110. */
  111. public static Date getServerStartDate()
  112. {
  113. long time = ManagementFactory.getRuntimeMXBean().getStartTime();
  114. return new Date(time);
  115. }
  116. /**
  117. * 计算两个时间差
  118. */
  119. public static String getDatePoor(Date endDate, Date nowDate)
  120. {
  121. long nd = 1000 * 24 * 60 * 60;
  122. long nh = 1000 * 60 * 60;
  123. long nm = 1000 * 60;
  124. // long ns = 1000;
  125. // 获得两个时间的毫秒时间差异
  126. long diff = endDate.getTime() - nowDate.getTime();
  127. // 计算差多少天
  128. long day = diff / nd;
  129. // 计算差多少小时
  130. long hour = diff % nd / nh;
  131. // 计算差多少分钟
  132. long min = diff % nd % nh / nm;
  133. // 计算差多少秒//输出结果
  134. // long sec = diff % nd % nh % nm / ns;
  135. return day + "天" + hour + "小时" + min + "分钟";
  136. }
  137. /**
  138. * 常规自动日期格式识别
  139. * @param str 时间字符串
  140. * @return Date
  141. * @author dc
  142. */
  143. public static String getDateFormat(String str) throws ParseException {
  144. // if (str.matches("[0-9]+")){
  145. // SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 日期格式
  146. // Date date = dateFormat.parse("1900-01-01 00:00:00"); // 指定日期
  147. // Date newDate = addDate(date, Long.parseLong(str)); // 指定日期加上天
  148. // str = new SimpleDateFormat("yyyy-MM-dd").format(newDate);
  149. // }
  150. boolean year = false;
  151. Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
  152. if (pattern.matcher(str.substring(0, 4)).matches()) {
  153. year = true;
  154. }
  155. StringBuilder sb = new StringBuilder();
  156. int index = 0;
  157. if (!year) {
  158. if (str.contains("月") || str.contains("-") || str.contains("/") || str.contains(".")) {
  159. if (Character.isDigit(str.charAt(0))) {
  160. index = 1;
  161. }
  162. } else {
  163. index = 3;
  164. }
  165. }
  166. for (int i = 0; i < str.length(); i++) {
  167. char chr = str.charAt(i);
  168. if (Character.isDigit(chr)) {
  169. if (index == 0) {
  170. sb.append("y");
  171. }
  172. if (index == 1) {
  173. sb.append("M");
  174. }
  175. if (index == 2) {
  176. sb.append("d");
  177. }
  178. if (index == 3) {
  179. sb.append("H");
  180. }
  181. if (index == 4) {
  182. sb.append("m");
  183. }
  184. if (index == 5) {
  185. sb.append("s");
  186. }
  187. if (index == 6) {
  188. sb.append("S");
  189. }
  190. } else {
  191. if (i > 0) {
  192. char lastChar = str.charAt(i - 1);
  193. if (Character.isDigit(lastChar)) {
  194. index++;
  195. }
  196. }
  197. sb.append(chr);
  198. }
  199. }
  200. return sb.toString();
  201. }
  202. public static Date addDate(Date date, long day) throws ParseException {
  203. long time = date.getTime(); // 得到指定日期的毫秒数
  204. day = day * 24 * 60 * 60 * 1000; // 要加上的天数转换成毫秒数
  205. time += day; // 相加得到新的毫秒数
  206. return new Date(time); // 将毫秒数转换成日期
  207. }
  208. public static int compareToDay(Date date1, Date date2) {
  209. DateFormat df = new SimpleDateFormat("yyyyMMdd");
  210. int d1 = Integer.parseInt(df.format(date1));
  211. int d2 = Integer.parseInt(df.format(date2));
  212. return Integer.compare(d1, d2);
  213. }
  214. }