DateUtils.java 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. package com.jsh.erp.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.time.*;
  7. import java.time.format.DateTimeFormatter;
  8. import java.time.format.DateTimeParseException;
  9. import java.util.*;
  10. /**
  11. * 时间工具类
  12. *
  13. * @author ruoyi
  14. */
  15. public class DateUtils extends org.apache.commons.lang3.time.DateUtils
  16. {
  17. public static String YYYY = "yyyy";
  18. public static String YYYY_MM = "yyyy-MM";
  19. public static String YYYY_MM_DD = "yyyy-MM-dd";
  20. public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
  21. public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
  22. private static String[] parsePatterns = {
  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. "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};
  26. /**
  27. * 获取当前Date型日期
  28. *
  29. * @return Date() 当前日期
  30. */
  31. public static Date getNowDate()
  32. {
  33. return new Date();
  34. }
  35. /**
  36. * 获取当前日期, 默认格式为yyyy-MM-dd
  37. *
  38. * @return String
  39. */
  40. public static String getDate()
  41. {
  42. return dateTimeNow(YYYY_MM_DD);
  43. }
  44. public static final String getTime()
  45. {
  46. return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
  47. }
  48. public static final String dateTimeNow()
  49. {
  50. return dateTimeNow(YYYYMMDDHHMMSS);
  51. }
  52. public static final String dateTimeNow(final String format)
  53. {
  54. return parseDateToStr(format, new Date());
  55. }
  56. public static final String dateTime(final Date date)
  57. {
  58. return parseDateToStr(YYYY_MM_DD, date);
  59. }
  60. public static final String parseDateToStr(final String format, final Date date)
  61. {
  62. return new SimpleDateFormat(format).format(date);
  63. }
  64. public static final Date dateTime(final String format, final String ts)
  65. {
  66. try
  67. {
  68. return new SimpleDateFormat(format).parse(ts);
  69. }
  70. catch (ParseException e)
  71. {
  72. throw new RuntimeException(e);
  73. }
  74. }
  75. /**
  76. * 日期路径 即年/月/日 如2018/08/08
  77. */
  78. public static final String datePath()
  79. {
  80. Date now = new Date();
  81. return DateFormatUtils.format(now, "yyyy/MM/dd");
  82. }
  83. /**
  84. * 日期路径 即年/月/日 如20180808
  85. */
  86. public static final String dateTime()
  87. {
  88. Date now = new Date();
  89. return DateFormatUtils.format(now, "yyyyMMdd");
  90. }
  91. /**
  92. * 日期型字符串转化为日期 格式
  93. */
  94. public static Date parseDate(Object str)
  95. {
  96. if (str == null)
  97. {
  98. return null;
  99. }
  100. try
  101. {
  102. return parseDate(str.toString(), parsePatterns);
  103. }
  104. catch (ParseException e)
  105. {
  106. return null;
  107. }
  108. }
  109. /**
  110. * 获取服务器启动时间
  111. */
  112. public static Date getServerStartDate()
  113. {
  114. long time = ManagementFactory.getRuntimeMXBean().getStartTime();
  115. return new Date(time);
  116. }
  117. /**
  118. * 计算相差天数
  119. */
  120. public static int differentDaysByMillisecond(Date date1, Date date2)
  121. {
  122. return Math.abs((int) ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24)));
  123. }
  124. /**
  125. * 计算时间差
  126. *
  127. * @param endDate 最后时间
  128. * @param startTime 开始时间
  129. * @return 时间差(天/小时/分钟)
  130. */
  131. public static String timeDistance(Date endDate, Date startTime)
  132. {
  133. long nd = 1000 * 24 * 60 * 60;
  134. long nh = 1000 * 60 * 60;
  135. long nm = 1000 * 60;
  136. // long ns = 1000;
  137. // 获得两个时间的毫秒时间差异
  138. long diff = endDate.getTime() - startTime.getTime();
  139. // 计算差多少天
  140. long day = diff / nd;
  141. // 计算差多少小时
  142. long hour = diff % nd / nh;
  143. // 计算差多少分钟
  144. long min = diff % nd % nh / nm;
  145. // 计算差多少秒//输出结果
  146. // long sec = diff % nd % nh % nm / ns;
  147. return day + "天" + hour + "小时" + min + "分钟";
  148. }
  149. /**
  150. * 增加 LocalDateTime ==> Date
  151. */
  152. public static Date toDate(LocalDateTime temporalAccessor)
  153. {
  154. ZonedDateTime zdt = temporalAccessor.atZone(ZoneId.systemDefault());
  155. return Date.from(zdt.toInstant());
  156. }
  157. /**
  158. * 增加 LocalDate ==> Date
  159. */
  160. public static Date toDate(LocalDate temporalAccessor)
  161. {
  162. LocalDateTime localDateTime = LocalDateTime.of(temporalAccessor, LocalTime.of(0, 0, 0));
  163. ZonedDateTime zdt = localDateTime.atZone(ZoneId.systemDefault());
  164. return Date.from(zdt.toInstant());
  165. }
  166. /**
  167. * 增加 LocalDate ==> Date 获取最早时刻
  168. *
  169. * @param temporalAccessor 日期
  170. * @return 列如:2025-01-01T00:00
  171. */
  172. public static Date toDateMin(LocalDate temporalAccessor)
  173. {
  174. LocalDateTime localDateTime = temporalAccessor.atStartOfDay();
  175. ZonedDateTime zdt = localDateTime.atZone(ZoneId.systemDefault());
  176. return Date.from(zdt.toInstant());
  177. }
  178. /**
  179. * 增加 LocalDate ==> Date 获取最晚时刻
  180. *
  181. * @param temporalAccessor 日期
  182. * @return 列如:2025-01-01T23:59:59
  183. */
  184. public static Date toDateMax(LocalDate temporalAccessor)
  185. {
  186. LocalDateTime localDateTime = temporalAccessor.atTime(LocalTime.MAX);
  187. ZonedDateTime zdt = localDateTime.atZone(ZoneId.systemDefault());
  188. return Date.from(zdt.toInstant());
  189. }
  190. /**
  191. * 增加 Date ==> LocalDate
  192. *
  193. * @param date 日期
  194. * @return
  195. */
  196. public static LocalDate toLocalDate(Date date)
  197. {
  198. if(!Optional.ofNullable(date).isPresent()){
  199. return LocalDate.now();
  200. }
  201. return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
  202. }
  203. /**
  204. * 增加 LocalDate ==> String
  205. *
  206. * @param date 日期
  207. * @param format 格式
  208. * @return
  209. */
  210. public static String localDate2String(LocalDate date,final String format) {
  211. DateTimeFormatter fmt = DateTimeFormatter.ofPattern(format);
  212. return date.format(fmt);
  213. }
  214. /**
  215. * 当前日期前或者后多少天
  216. * day 负数往历史日期,正数往未来
  217. */
  218. public static Date displacement(Integer day){
  219. // 获取当前时间
  220. Calendar calendar = Calendar.getInstance();
  221. Date currentTime = new Date();
  222. calendar.setTime(currentTime);
  223. calendar.add(Calendar.DAY_OF_MONTH, day);
  224. return calendar.getTime();
  225. }
  226. /**
  227. * 根据当前的时间区间获取上一周期的时间区间
  228. * @param beginTime 开始时间
  229. * @param endTime 结束时间
  230. * @return
  231. */
  232. public static String[] getFrontCycleDate(String beginTime, String endTime) {
  233. //处理上一周期时间
  234. LocalDate localDateBeginTime = LocalDate.parse(beginTime);
  235. LocalDate localDateEndTime = LocalDate.parse(endTime);
  236. //获取上一周期结束时间
  237. LocalDate frontEndTime = localDateBeginTime.minusDays(1);
  238. LocalDate frontBeginTime;
  239. //根据筛选时间天数获取上一周期开始时间
  240. int days = DateUtils.differentDaysByMillisecond(DateUtils.toDate(localDateBeginTime), DateUtils.toDate(localDateEndTime));
  241. if (days == 0) {
  242. frontBeginTime = localDateBeginTime.minusDays(1);
  243. } else {
  244. frontBeginTime = localDateBeginTime.minusDays(days + 1);
  245. }
  246. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
  247. String beginTimeString = frontBeginTime.format(formatter);
  248. String endTimeString = frontEndTime.format(formatter);
  249. return new String[]{beginTimeString, endTimeString};
  250. }
  251. /**
  252. * 获取两个日期之间的所有日期
  253. *
  254. * @param startDateStr 开始日期字符串,格式为yyyy-MM-dd
  255. * @param endDateStr 结束日期字符串,格式为yyyy-MM-dd
  256. * @return 日期列表
  257. */
  258. public static List<LocalDate> getDatesBetween(String startDateStr, String endDateStr) {
  259. // 定义日期格式
  260. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
  261. // 将字符串日期转换为LocalDate
  262. LocalDate startDate = LocalDate.parse(startDateStr, formatter);
  263. LocalDate endDate = LocalDate.parse(endDateStr, formatter);
  264. // 创建一个列表来存储日期
  265. List<LocalDate> dates = new ArrayList<>();
  266. // 使用while循环获取两个日期之间的所有日期
  267. LocalDate currentDate = startDate;
  268. while (!currentDate.isAfter(endDate)) {
  269. dates.add(currentDate);
  270. currentDate = currentDate.plusDays(1); // 将当前日期加一天
  271. }
  272. return dates;
  273. }
  274. public static List<LocalDate> getDatesBetween(LocalDate startDate, LocalDate endDate) {
  275. // 创建一个列表来存储日期
  276. List<LocalDate> dates = new ArrayList<>();
  277. // 使用while循环获取两个日期之间的所有日期
  278. LocalDate currentDate = startDate;
  279. while (!currentDate.isAfter(endDate)) {
  280. dates.add(currentDate);
  281. currentDate = currentDate.plusDays(1); // 将当前日期加一天
  282. }
  283. return dates;
  284. }
  285. /**
  286. * 校验字符日期格式是否正确
  287. */
  288. public static boolean isDateValid(String inputDate, String format) {
  289. try {
  290. DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
  291. LocalDate.parse(inputDate, formatter);
  292. } catch (DateTimeParseException e) {
  293. return false;
  294. }
  295. return true;
  296. }
  297. }