DateUtils.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. package com.goafanti.common.utils;
  2. import com.goafanti.common.constant.AFTConstants;
  3. import com.goafanti.common.error.BusinessException;
  4. import org.apache.commons.lang3.time.DateFormatUtils;
  5. import java.text.ParseException;
  6. import java.text.SimpleDateFormat;
  7. import java.time.*;
  8. import java.time.temporal.ChronoUnit;
  9. import java.time.temporal.TemporalAdjusters;
  10. import java.util.Calendar;
  11. import java.util.Date;
  12. public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
  13. public static final String YEAR_SPAN = "yearSpan";
  14. public static final String QUARTER_SPAN = "quarterSpan";
  15. public static final String MONTH_SPAN = "monthSpan";
  16. public static final String WEEK_SPAN = "weekSpan";
  17. public static final String DAY_SPAN = "daySpan";
  18. private static String[] parsePatterns = {
  19. "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
  20. "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
  21. "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM",
  22. "yyyy年MM月dd日", "yyyy年MM月dd日 HH:mm:ss", "yyyy年MM月dd日 HH:mm", "yyyy年MM月",};
  23. /**
  24. * Determines how two dates compare up to no more than the specified most
  25. * significant field.
  26. *
  27. * @param date1
  28. * the first date, not <code>null</code>
  29. * @param date2
  30. * the second date, not <code>null</code>
  31. * @param field
  32. * the field from <code>Calendar</code>
  33. * @return diff millis
  34. * @throws IllegalArgumentException
  35. * if any argument is <code>null</code>
  36. * @see #truncate(Calendar, int)
  37. * @see #truncatedCompareTo(Date, Date, int)
  38. * @since 3.0
  39. */
  40. public static long truncatedDiffTo(final Date date1, final Date date2, final int field) {
  41. final Date truncatedDate1 = truncate(date1, field);
  42. final Date truncatedDate2 = truncate(date2, field);
  43. long thisTime = truncatedDate1.getTime();
  44. long anotherTime = truncatedDate2.getTime();
  45. return Math.abs(thisTime - anotherTime);
  46. }
  47. /**
  48. * Determines how two dates compare up to no more than the specified most
  49. * significant field.
  50. *
  51. * @param date1
  52. * the first date, not <code>null</code>
  53. * @param date2
  54. * the second date, not <code>null</code>
  55. * the field from <code>Calendar</code>
  56. * @return diff millis
  57. * @throws IllegalArgumentException
  58. * if any argument is <code>null</code>
  59. * @see #truncate(Calendar, int)
  60. * @see #truncatedCompareTo(Date, Date, int)
  61. * @since 3.0
  62. */
  63. public static long truncatedHourDiffTo(final Date date1, final Date date2) {
  64. return truncatedDiffTo(date1, date2, Calendar.HOUR) / 3600000;
  65. }
  66. /**
  67. * Determines how two dates compare up to no more than the specified most
  68. * significant field.
  69. *
  70. * @param date1
  71. * the first date, not <code>null</code>
  72. * @param date2
  73. * the second date, not <code>null</code>
  74. * the field from <code>Calendar</code>
  75. * @return diff millis
  76. * @throws IllegalArgumentException
  77. * if any argument is <code>null</code>
  78. * @see #truncate(Calendar, int)
  79. * @see #truncatedCompareTo(Date, Date, int)
  80. * @since 3.0
  81. */
  82. public static long truncatedMinuteDiffTo(final Date date1, final Date date2) {
  83. return truncatedDiffTo(date1, date2, Calendar.MINUTE) / 60000;
  84. }
  85. /**
  86. * 得到某年某周的第一天
  87. *
  88. * @param year
  89. * @param week
  90. * @return
  91. */
  92. public static Date getFirstDayOfWeek(int year, int week) {
  93. week = week - 1;
  94. Calendar calendar = Calendar.getInstance();
  95. calendar.set(Calendar.YEAR, year);
  96. calendar.set(Calendar.MONTH, Calendar.JANUARY);
  97. calendar.set(Calendar.DATE, 1);
  98. Calendar cal = (Calendar) calendar.clone();
  99. cal.add(Calendar.DATE, week * 7);
  100. return getFirstDayOfWeek(cal.getTime());
  101. }
  102. /**
  103. * 得到某年某周的最后一天
  104. *
  105. * @param year
  106. * @param week
  107. * @return
  108. */
  109. public static Date getLastDayOfWeek(int year, int week) {
  110. week = week - 1;
  111. Calendar calendar = Calendar.getInstance();
  112. calendar.set(Calendar.YEAR, year);
  113. calendar.set(Calendar.MONTH, Calendar.JANUARY);
  114. calendar.set(Calendar.DATE, 1);
  115. Calendar cal = (Calendar) calendar.clone();
  116. cal.add(Calendar.DATE, week * 7);
  117. return getLastDayOfWeek(cal.getTime());
  118. }
  119. /**
  120. * 取得当前日期所在周的第一天
  121. *
  122. * @param date
  123. * @return
  124. */
  125. public static Date getFirstDayOfWeek(Date date) {
  126. Calendar calendar = Calendar.getInstance();
  127. calendar.setFirstDayOfWeek(Calendar.SUNDAY);
  128. calendar.setTime(date);
  129. calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek()); // Sunday
  130. calendar.add(Calendar.DATE, 1);
  131. setMidnight(calendar);
  132. return calendar.getTime();
  133. }
  134. /**
  135. * 取得当前日期所在周的最后一天
  136. *
  137. * @param date
  138. * @return
  139. */
  140. public static Date getLastDayOfWeek(Date date) {
  141. Calendar calendar = Calendar.getInstance();
  142. calendar.setFirstDayOfWeek(Calendar.SUNDAY);
  143. calendar.setTime(date);
  144. calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek() + 6); // Saturday
  145. setMidnight(calendar);
  146. return calendar.getTime();
  147. }
  148. /**
  149. * 取得当前日期所在周的前一周最后一天
  150. *
  151. * @param date
  152. * @return
  153. */
  154. public static Date getLastDayOfLastWeek(Date date) {
  155. Calendar calendar = Calendar.getInstance();
  156. calendar.setTime(date);
  157. return getLastDayOfWeek(calendar.get(Calendar.YEAR), calendar.get(Calendar.WEEK_OF_YEAR) - 1);
  158. }
  159. /**
  160. * 返回指定日期的月的第一天
  161. *
  162. * @return
  163. */
  164. public static Date getFirstDayOfMonth(Date date) {
  165. Calendar calendar = Calendar.getInstance();
  166. calendar.setTime(date);
  167. calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), 1);
  168. setMidnight(calendar);
  169. return calendar.getTime();
  170. }
  171. /**
  172. * 返回指定年月的月的第一天
  173. *
  174. * @param year
  175. * @param month
  176. * @return
  177. */
  178. public static Date getFirstDayOfMonth(Integer year, Integer month) {
  179. Calendar calendar = Calendar.getInstance();
  180. if (year == null) {
  181. year = calendar.get(Calendar.YEAR);
  182. }
  183. if (month == null) {
  184. month = calendar.get(Calendar.MONTH);
  185. }
  186. calendar.set(year, month, 1);
  187. setMidnight(calendar);
  188. return calendar.getTime();
  189. }
  190. /**
  191. * 返回指定日期的月的最后一天
  192. *
  193. * @return
  194. */
  195. public static Date getLastDayOfMonth(Date date) {
  196. Calendar calendar = Calendar.getInstance();
  197. calendar.setTime(date);
  198. calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), 1);
  199. calendar.roll(Calendar.DATE, -1);
  200. setMidnight(calendar);
  201. return calendar.getTime();
  202. }
  203. /**
  204. * 返回指定年月的月的最后一天
  205. *
  206. * @param year
  207. * @param month
  208. * @return
  209. */
  210. public static Date getLastDayOfMonth(Integer year, Integer month) {
  211. Calendar calendar = Calendar.getInstance();
  212. if (year == null) {
  213. year = calendar.get(Calendar.YEAR);
  214. }
  215. if (month == null) {
  216. month = calendar.get(Calendar.MONTH);
  217. }
  218. calendar.set(year, month, 1);
  219. calendar.roll(Calendar.DATE, -1);
  220. setMidnight(calendar);
  221. return calendar.getTime();
  222. }
  223. /**
  224. * 返回指定日期的上个月的最后一天
  225. *
  226. * @return
  227. */
  228. public static Date getLastDayOfLastMonth(Date date) {
  229. Calendar calendar = Calendar.getInstance();
  230. calendar.setTime(date);
  231. calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) - 1, 1);
  232. calendar.roll(Calendar.DATE, -1);
  233. setMidnight(calendar);
  234. return calendar.getTime();
  235. }
  236. /**
  237. * 返回指定日期的季的第一天
  238. *
  239. * @return
  240. */
  241. public static Date getFirstDayOfQuarter(Date date) {
  242. Calendar calendar = Calendar.getInstance();
  243. calendar.setTime(date);
  244. return getFirstDayOfQuarter(calendar.get(Calendar.YEAR), getQuarterOfYear(date));
  245. }
  246. /**
  247. * 返回指定年季的季的第一天
  248. *
  249. * @param year
  250. * @param quarter
  251. * @return
  252. */
  253. public static Date getFirstDayOfQuarter(Integer year, Integer quarter) {
  254. Calendar calendar = Calendar.getInstance();
  255. Integer month = 0;
  256. if (quarter == 1) {
  257. month = 1 - 1;
  258. } else if (quarter == 2) {
  259. month = 4 - 1;
  260. } else if (quarter == 3) {
  261. month = 7 - 1;
  262. } else if (quarter == 4) {
  263. month = 10 - 1;
  264. } else {
  265. month = calendar.get(Calendar.MONTH);
  266. }
  267. return getFirstDayOfMonth(year, month);
  268. }
  269. /**
  270. * 返回指定日期的季的最后一天
  271. *
  272. * @return
  273. */
  274. public static Date getLastDayOfQuarter(Date date) {
  275. Calendar calendar = Calendar.getInstance();
  276. calendar.setTime(date);
  277. return getLastDayOfQuarter(calendar.get(Calendar.YEAR), getQuarterOfYear(date));
  278. }
  279. /**
  280. * 返回指定年季的季的最后一天
  281. *
  282. * @param year
  283. * @param quarter
  284. * @return
  285. */
  286. public static Date getLastDayOfQuarter(Integer year, Integer quarter) {
  287. Calendar calendar = Calendar.getInstance();
  288. Integer month = 0;
  289. if (quarter == 1) {
  290. month = 3 - 1;
  291. } else if (quarter == 2) {
  292. month = 6 - 1;
  293. } else if (quarter == 3) {
  294. month = 9 - 1;
  295. } else if (quarter == 4) {
  296. month = 12 - 1;
  297. } else {
  298. month = calendar.get(Calendar.MONTH);
  299. }
  300. return getLastDayOfMonth(year, month);
  301. }
  302. /**
  303. * 返回指定日期的上一季的最后一天
  304. *
  305. * @return
  306. */
  307. public static Date getLastDayOfLastQuarter(Date date) {
  308. Calendar calendar = Calendar.getInstance();
  309. calendar.setTime(date);
  310. return getLastDayOfLastQuarter(calendar.get(Calendar.YEAR), getQuarterOfYear(date));
  311. }
  312. /**
  313. * 返回指定年季的上一季的最后一天
  314. *
  315. * @param year
  316. * @param quarter
  317. * @return
  318. */
  319. public static Date getLastDayOfLastQuarter(Integer year, Integer quarter) {
  320. Calendar calendar = Calendar.getInstance();
  321. Integer month = 0;
  322. if (quarter == 1) {
  323. month = 12 - 1;
  324. } else if (quarter == 2) {
  325. month = 3 - 1;
  326. } else if (quarter == 3) {
  327. month = 6 - 1;
  328. } else if (quarter == 4) {
  329. month = 9 - 1;
  330. } else {
  331. month = calendar.get(Calendar.MONTH);
  332. }
  333. return getLastDayOfMonth(year, month);
  334. }
  335. /**
  336. * 返回指定日期的季度
  337. *
  338. * @param date
  339. * @return
  340. */
  341. public static int getQuarterOfYear(Date date) {
  342. Calendar calendar = Calendar.getInstance();
  343. calendar.setTime(date);
  344. return calendar.get(Calendar.MONTH) / 3 + 1;
  345. }
  346. /**
  347. * 返回昨天日期
  348. * @return date
  349. */
  350. public static Date getYesterday() {
  351. Calendar calendar = Calendar.getInstance();
  352. calendar.add(Calendar.DATE, -1);
  353. setMidnight(calendar);
  354. return calendar.getTime();
  355. }
  356. /**
  357. * 返回前一年最后一天
  358. * @param date
  359. * @return
  360. */
  361. public static Date getLastDayOfLastYear(Date date) {
  362. Calendar calendar = Calendar.getInstance();
  363. return getLastDayOfYear(calendar.get(Calendar.YEAR) - 1);
  364. }
  365. /**
  366. * 返回指定年的最后一天
  367. * @param year
  368. * @return
  369. */
  370. public static Date getLastDayOfYear(int year) {
  371. Calendar calendar = Calendar.getInstance();
  372. calendar.set(Calendar.YEAR, year - 1);
  373. calendar.roll(Calendar.DAY_OF_YEAR, -1);
  374. setMidnight(calendar);
  375. return calendar.getTime();
  376. }
  377. /**
  378. * 格式化时间
  379. * @param date
  380. * @param pattern
  381. * @return
  382. */
  383. public static String formatDate(Date date, String pattern) {
  384. SimpleDateFormat format = new SimpleDateFormat(pattern);
  385. return format.format(date);
  386. }
  387. public static String formatDateYYYYMMdd(Date date, String pattern) {
  388. SimpleDateFormat format = new SimpleDateFormat(parsePatterns[1]);
  389. return format.format(date);
  390. }
  391. public static String formatDateYYYYMMddHHmm(Date date) {
  392. SimpleDateFormat format = new SimpleDateFormat(parsePatterns[2]);
  393. return format.format(date);
  394. }
  395. public static String formatDateChineseYYYYMMddHH(Date date) {
  396. SimpleDateFormat format = new SimpleDateFormat(parsePatterns[14]);
  397. return format.format(date);
  398. }
  399. /**
  400. * 解析日期
  401. * @param source
  402. * @param pattern
  403. * @return
  404. * @throws ParseException
  405. */
  406. public static Date parseDate(String source, String pattern) throws ParseException {
  407. SimpleDateFormat format = new SimpleDateFormat(pattern);
  408. return format.parse(source);
  409. }
  410. /**
  411. * 时分秒毫秒归零
  412. * @param c
  413. * @return
  414. */
  415. public static Calendar setMidnight(Calendar c) {
  416. c.set(Calendar.HOUR_OF_DAY, 0);
  417. c.set(Calendar.MINUTE, 0);
  418. c.set(Calendar.SECOND, 0);
  419. c.set(Calendar.MILLISECOND, 0);
  420. return c;
  421. }
  422. /**
  423. * 时分秒毫秒归零
  424. * @return
  425. */
  426. public static Date getEarlyToday() {
  427. Calendar c = Calendar.getInstance();
  428. setMidnight(c);
  429. return c.getTime();
  430. }
  431. /**
  432. * 字符串转日期
  433. * @param date
  434. * @param format
  435. * @return
  436. * @throws ParseException
  437. */
  438. public static Date StringToDate( String date, String format) {
  439. Date date2;
  440. try {
  441. date2=new SimpleDateFormat(format).parse(date);
  442. } catch (ParseException e) {
  443. throw new BusinessException("时间转换失败,date="+date,e);
  444. }
  445. return date2;
  446. }
  447. public static final String parseDateToStr(final String format, final Date date) {
  448. return new SimpleDateFormat(format).format(date);
  449. }
  450. /**
  451. * 增加 LocalDate ==> Date
  452. */
  453. public static Date toDate(LocalDate temporalAccessor)
  454. {
  455. LocalDateTime localDateTime = LocalDateTime.of(temporalAccessor, LocalTime.of(0, 0, 0));
  456. ZonedDateTime zdt = localDateTime.atZone(ZoneId.systemDefault());
  457. return Date.from(zdt.toInstant());
  458. }
  459. /**
  460. * 日期路径 即年/月/日 如2018/08/08
  461. */
  462. public static final String datePath()
  463. {
  464. Date now = new Date();
  465. return DateFormatUtils.format(now, "yyyy/MM/dd");
  466. }
  467. /**
  468. * 日期型字符串转化为日期 格式
  469. */
  470. public static Date parseDate(Object str)
  471. {
  472. if (str == null)
  473. {
  474. return null;
  475. }
  476. try
  477. {
  478. return parseDate(str.toString(), parsePatterns);
  479. }
  480. catch (ParseException e)
  481. {
  482. return null;
  483. }
  484. }
  485. /**
  486. * 计算距离今天天数
  487. * @param dates
  488. * @return
  489. */
  490. public static Integer sumDays(String dates) {
  491. Date date = StringToDate(dates, AFTConstants.YYYYMMDDHHMMSS);
  492. Date now=new Date();
  493. int days=(int)((now.getTime()-date.getTime())/(1000*3600*24));
  494. return days;
  495. }
  496. /**
  497. * 获取当前时间
  498. * @return
  499. */
  500. public static LocalDate getToDay() {
  501. return LocalDate.now();
  502. }
  503. public static LocalDate gettomorrow() {
  504. LocalDate today = LocalDate.now();
  505. return today.plusDays(1);
  506. }
  507. public static LocalDate getFirstDayOfThisWeek() {
  508. LocalDate today = LocalDate.now();
  509. return today.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
  510. }
  511. public static long getDaysBetween(Date startDate, Date endDate) {
  512. // 将Date转换为LocalDate(需指定时区)
  513. LocalDate start = startDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
  514. LocalDate end = endDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
  515. // 计算天数差(end - start)
  516. return ChronoUnit.DAYS.between(start, end);
  517. }
  518. public static void main(String[] args) {
  519. Date date = StringToDate("2023-08-08 09:00:00", AFTConstants.YYYYMMDDHHMMSS);
  520. Date date1 = StringToDate("2023-08-12 06:00:00", AFTConstants.YYYYMMDDHHMMSS);
  521. long daysBetween = getDaysBetween(date, date1);
  522. daysBetween+=1;
  523. System.out.println(daysBetween);
  524. for (int i = 0; i <daysBetween; i++) {
  525. LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
  526. LocalDate localDate1 = localDate.plusDays(i);
  527. System.out.println(localDate1);
  528. }
  529. }
  530. }