DateUtils.java 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. package com.ruoyi.common.utils;
  2. import org.apache.commons.lang3.time.DateFormatUtils;
  3. import java.lang.management.ManagementFactory;
  4. import java.text.ParseException;
  5. import java.text.SimpleDateFormat;
  6. import java.util.*;
  7. /**
  8. * 时间工具类
  9. *
  10. * @author ruoyi
  11. */
  12. public class DateUtils extends org.apache.commons.lang3.time.DateUtils
  13. {
  14. public static String YYYY = "yyyy";
  15. public static String MM = "MM";
  16. public static String dd = "dd";
  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 getDay()
  44. {
  45. return dateTimeNow(dd);
  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 long getDateDay(Date endDate, Date nowDate)
  124. {
  125. long nd = 1000 * 24 * 60 * 60;
  126. long nh = 1000 * 60 * 60;
  127. long nm = 1000 * 60;
  128. // long ns = 1000;
  129. // 获得两个时间的毫秒时间差异
  130. long diff = endDate.getTime() - nowDate.getTime();
  131. // 计算差多少天
  132. long day = diff / nd + 1L;
  133. return day;
  134. }
  135. /**
  136. * 计算两个时间差
  137. */
  138. public static String getDatePoor(Date endDate, Date nowDate)
  139. {
  140. long nd = 1000 * 24 * 60 * 60;
  141. long nh = 1000 * 60 * 60;
  142. long nm = 1000 * 60;
  143. // long ns = 1000;
  144. // 获得两个时间的毫秒时间差异
  145. long diff = endDate.getTime() - nowDate.getTime();
  146. // 计算差多少天
  147. long day = diff / nd;
  148. // 计算差多少小时
  149. long hour = diff % nd / nh;
  150. // 计算差多少分钟
  151. long min = diff % nd % nh / nm;
  152. // 计算差多少秒//输出结果
  153. // long sec = diff % nd % nh % nm / ns;
  154. return day + "天" + hour + "小时" + min + "分钟";
  155. }
  156. /**
  157. * 当前日期往前往后推x天
  158. */
  159. public static Date dateAdd(Date date, int num)
  160. {
  161. Calendar calendar = new GregorianCalendar();
  162. calendar.setTime(date);
  163. calendar.add(Calendar.DATE,num);
  164. return calendar.getTime();
  165. }
  166. /**
  167. * 获取当天开始与结束日期
  168. * @return
  169. */
  170. public static List<String> getCurrentDay()
  171. {
  172. List<String> stringList = new ArrayList<>();
  173. Calendar cale = null;
  174. cale = Calendar.getInstance();
  175. // 获取当月第一天和最后一天
  176. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  177. String firstday, lastday;
  178. // 获取前月的第一天
  179. cale = Calendar.getInstance();
  180. firstday = format.format(cale.getTime()) + " 00:00:00";
  181. stringList.add(firstday);
  182. // 获取前月的最后一天
  183. cale = Calendar.getInstance();
  184. lastday = format.format(cale.getTime()) + " 23:59:59";
  185. stringList.add(lastday);
  186. return stringList;
  187. }
  188. /**
  189. * 获取当天开始与结束日期
  190. * @return
  191. */
  192. public static List<String> getSpecifyDay(Date date)
  193. {
  194. List<String> stringList = new ArrayList<>();
  195. SimpleDateFormat format = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
  196. String firstday, lastday;
  197. Calendar calendar = Calendar.getInstance();
  198. calendar.setTime(date);
  199. calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
  200. calendar.set(Calendar.MILLISECOND, 0);
  201. Date dete1 = new Date(calendar.getTimeInMillis());//得到指定日期的开始时间
  202. calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
  203. calendar.set(Calendar.MILLISECOND, 999);
  204. Date dete2 = new Date(calendar.getTimeInMillis());//得到指定日期的结束时间
  205. firstday = format.format(dete1.getTime());
  206. stringList.add(firstday);
  207. lastday = format.format(dete2.getTime());
  208. stringList.add(lastday);
  209. return stringList;
  210. }
  211. /**
  212. * 获取当前月份第一天与最后一天
  213. * @return
  214. */
  215. public static List<String> getMonth()
  216. {
  217. List<String> stringList = new ArrayList<>();
  218. Calendar cale = null;
  219. cale = Calendar.getInstance();
  220. // 获取当月第一天和最后一天
  221. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  222. String firstday, lastday;
  223. // 获取前月的第一天
  224. cale = Calendar.getInstance();
  225. cale.add(Calendar.MONTH, 0);
  226. cale.set(Calendar.DAY_OF_MONTH, 1);
  227. firstday = format.format(cale.getTime()) + " 00:00:00";
  228. stringList.add(firstday);
  229. // 获取前月的最后一天
  230. cale = Calendar.getInstance();
  231. cale.add(Calendar.MONTH, 1);
  232. cale.set(Calendar.DAY_OF_MONTH, 0);
  233. lastday = format.format(cale.getTime()) + " 23:59:59";
  234. stringList.add(lastday);
  235. return stringList;
  236. }
  237. }