DateUtils.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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. setMidnight(calendar);
  131. return calendar.getTime();
  132. }
  133. /**
  134. * 取得当前日期所在周的最后一天
  135. *
  136. * @param date
  137. * @return
  138. */
  139. public static Date getLastDayOfWeek(Date date) {
  140. Calendar calendar = Calendar.getInstance();
  141. calendar.setFirstDayOfWeek(Calendar.SUNDAY);
  142. calendar.setTime(date);
  143. calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek() + 6); // Saturday
  144. setMidnight(calendar);
  145. return calendar.getTime();
  146. }
  147. /**
  148. * 取得当前日期所在周的前一周最后一天
  149. *
  150. * @param date
  151. * @return
  152. */
  153. public static Date getLastDayOfLastWeek(Date date) {
  154. Calendar calendar = Calendar.getInstance();
  155. calendar.setTime(date);
  156. return getLastDayOfWeek(calendar.get(Calendar.YEAR), calendar.get(Calendar.WEEK_OF_YEAR) - 1);
  157. }
  158. /**
  159. * 返回指定日期的月的第一天
  160. *
  161. * @return
  162. */
  163. public static Date getFirstDayOfMonth(Date date) {
  164. Calendar calendar = Calendar.getInstance();
  165. calendar.setTime(date);
  166. calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), 1);
  167. setMidnight(calendar);
  168. return calendar.getTime();
  169. }
  170. /**
  171. * 返回指定年月的月的第一天
  172. *
  173. * @param year
  174. * @param month
  175. * @return
  176. */
  177. public static Date getFirstDayOfMonth(Integer year, Integer month) {
  178. Calendar calendar = Calendar.getInstance();
  179. if (year == null) {
  180. year = calendar.get(Calendar.YEAR);
  181. }
  182. if (month == null) {
  183. month = calendar.get(Calendar.MONTH);
  184. }
  185. calendar.set(year, month, 1);
  186. setMidnight(calendar);
  187. return calendar.getTime();
  188. }
  189. /**
  190. * 返回指定日期的月的最后一天
  191. *
  192. * @return
  193. */
  194. public static Date getLastDayOfMonth(Date date) {
  195. Calendar calendar = Calendar.getInstance();
  196. calendar.setTime(date);
  197. calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), 1);
  198. calendar.roll(Calendar.DATE, -1);
  199. setMidnight(calendar);
  200. return calendar.getTime();
  201. }
  202. /**
  203. * 返回指定年月的月的最后一天
  204. *
  205. * @param year
  206. * @param month
  207. * @return
  208. */
  209. public static Date getLastDayOfMonth(Integer year, Integer month) {
  210. Calendar calendar = Calendar.getInstance();
  211. if (year == null) {
  212. year = calendar.get(Calendar.YEAR);
  213. }
  214. if (month == null) {
  215. month = calendar.get(Calendar.MONTH);
  216. }
  217. calendar.set(year, month, 1);
  218. calendar.roll(Calendar.DATE, -1);
  219. setMidnight(calendar);
  220. return calendar.getTime();
  221. }
  222. /**
  223. * 返回指定日期的上个月的最后一天
  224. *
  225. * @return
  226. */
  227. public static Date getLastDayOfLastMonth(Date date) {
  228. Calendar calendar = Calendar.getInstance();
  229. calendar.setTime(date);
  230. calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) - 1, 1);
  231. calendar.roll(Calendar.DATE, -1);
  232. setMidnight(calendar);
  233. return calendar.getTime();
  234. }
  235. /**
  236. * 返回指定日期的季的第一天
  237. *
  238. * @return
  239. */
  240. public static Date getFirstDayOfQuarter(Date date) {
  241. Calendar calendar = Calendar.getInstance();
  242. calendar.setTime(date);
  243. return getFirstDayOfQuarter(calendar.get(Calendar.YEAR), getQuarterOfYear(date));
  244. }
  245. /**
  246. * 返回指定年季的季的第一天
  247. *
  248. * @param year
  249. * @param quarter
  250. * @return
  251. */
  252. public static Date getFirstDayOfQuarter(Integer year, Integer quarter) {
  253. Calendar calendar = Calendar.getInstance();
  254. Integer month = 0;
  255. if (quarter == 1) {
  256. month = 1 - 1;
  257. } else if (quarter == 2) {
  258. month = 4 - 1;
  259. } else if (quarter == 3) {
  260. month = 7 - 1;
  261. } else if (quarter == 4) {
  262. month = 10 - 1;
  263. } else {
  264. month = calendar.get(Calendar.MONTH);
  265. }
  266. return getFirstDayOfMonth(year, month);
  267. }
  268. /**
  269. * 返回指定日期的季的最后一天
  270. *
  271. * @return
  272. */
  273. public static Date getLastDayOfQuarter(Date date) {
  274. Calendar calendar = Calendar.getInstance();
  275. calendar.setTime(date);
  276. return getLastDayOfQuarter(calendar.get(Calendar.YEAR), getQuarterOfYear(date));
  277. }
  278. /**
  279. * 返回指定年季的季的最后一天
  280. *
  281. * @param year
  282. * @param quarter
  283. * @return
  284. */
  285. public static Date getLastDayOfQuarter(Integer year, Integer quarter) {
  286. Calendar calendar = Calendar.getInstance();
  287. Integer month = 0;
  288. if (quarter == 1) {
  289. month = 3 - 1;
  290. } else if (quarter == 2) {
  291. month = 6 - 1;
  292. } else if (quarter == 3) {
  293. month = 9 - 1;
  294. } else if (quarter == 4) {
  295. month = 12 - 1;
  296. } else {
  297. month = calendar.get(Calendar.MONTH);
  298. }
  299. return getLastDayOfMonth(year, month);
  300. }
  301. /**
  302. * 返回指定日期的上一季的最后一天
  303. *
  304. * @return
  305. */
  306. public static Date getLastDayOfLastQuarter(Date date) {
  307. Calendar calendar = Calendar.getInstance();
  308. calendar.setTime(date);
  309. return getLastDayOfLastQuarter(calendar.get(Calendar.YEAR), getQuarterOfYear(date));
  310. }
  311. /**
  312. * 返回指定年季的上一季的最后一天
  313. *
  314. * @param year
  315. * @param quarter
  316. * @return
  317. */
  318. public static Date getLastDayOfLastQuarter(Integer year, Integer quarter) {
  319. Calendar calendar = Calendar.getInstance();
  320. Integer month = 0;
  321. if (quarter == 1) {
  322. month = 12 - 1;
  323. } else if (quarter == 2) {
  324. month = 3 - 1;
  325. } else if (quarter == 3) {
  326. month = 6 - 1;
  327. } else if (quarter == 4) {
  328. month = 9 - 1;
  329. } else {
  330. month = calendar.get(Calendar.MONTH);
  331. }
  332. return getLastDayOfMonth(year, month);
  333. }
  334. /**
  335. * 返回指定日期的季度
  336. *
  337. * @param date
  338. * @return
  339. */
  340. public static int getQuarterOfYear(Date date) {
  341. Calendar calendar = Calendar.getInstance();
  342. calendar.setTime(date);
  343. return calendar.get(Calendar.MONTH) / 3 + 1;
  344. }
  345. /**
  346. * 返回昨天日期
  347. * @return date
  348. */
  349. public static Date getYesterday() {
  350. Calendar calendar = Calendar.getInstance();
  351. calendar.add(Calendar.DATE, -1);
  352. setMidnight(calendar);
  353. return calendar.getTime();
  354. }
  355. /**
  356. * 返回前一年最后一天
  357. * @param date
  358. * @return
  359. */
  360. public static Date getLastDayOfLastYear(Date date) {
  361. Calendar calendar = Calendar.getInstance();
  362. return getLastDayOfYear(calendar.get(Calendar.YEAR) - 1);
  363. }
  364. /**
  365. * 返回指定年的最后一天
  366. * @param year
  367. * @return
  368. */
  369. public static Date getLastDayOfYear(int year) {
  370. Calendar calendar = Calendar.getInstance();
  371. calendar.set(Calendar.YEAR, year - 1);
  372. calendar.roll(Calendar.DAY_OF_YEAR, -1);
  373. setMidnight(calendar);
  374. return calendar.getTime();
  375. }
  376. /**
  377. * 格式化时间
  378. * @param date
  379. * @param pattern
  380. * @return
  381. */
  382. public static String formatDate(Date date, String pattern) {
  383. SimpleDateFormat format = new SimpleDateFormat(pattern);
  384. return format.format(date);
  385. }
  386. public static String formatDateYYYYMMdd(Date date, String pattern) {
  387. SimpleDateFormat format = new SimpleDateFormat(parsePatterns[1]);
  388. return format.format(date);
  389. }
  390. public static String formatDateYYYYMMddHHmm(Date date) {
  391. SimpleDateFormat format = new SimpleDateFormat(parsePatterns[2]);
  392. return format.format(date);
  393. }
  394. public static String formatDateChineseYYYYMMddHH(Date date) {
  395. SimpleDateFormat format = new SimpleDateFormat(parsePatterns[14]);
  396. return format.format(date);
  397. }
  398. /**
  399. * 解析日期
  400. * @param source
  401. * @param pattern
  402. * @return
  403. * @throws ParseException
  404. */
  405. public static Date parseDate(String source, String pattern) throws ParseException {
  406. SimpleDateFormat format = new SimpleDateFormat(pattern);
  407. return format.parse(source);
  408. }
  409. /**
  410. * 时分秒毫秒归零
  411. * @param c
  412. * @return
  413. */
  414. public static Calendar setMidnight(Calendar c) {
  415. c.set(Calendar.HOUR_OF_DAY, 0);
  416. c.set(Calendar.MINUTE, 0);
  417. c.set(Calendar.SECOND, 0);
  418. c.set(Calendar.MILLISECOND, 0);
  419. return c;
  420. }
  421. /**
  422. * 时分秒毫秒归零
  423. * @return
  424. */
  425. public static Date getEarlyToday() {
  426. Calendar c = Calendar.getInstance();
  427. setMidnight(c);
  428. return c.getTime();
  429. }
  430. /**
  431. * 字符串转日期
  432. * @param date
  433. * @param format
  434. * @return
  435. * @throws ParseException
  436. */
  437. public static Date StringToDate( String date, String format) {
  438. Date date2;
  439. try {
  440. date2=new SimpleDateFormat(format).parse(date);
  441. } catch (ParseException e) {
  442. throw new BusinessException("时间转换失败,date="+date,e);
  443. }
  444. return date2;
  445. }
  446. public static final String parseDateToStr(final String format, final Date date) {
  447. return new SimpleDateFormat(format).format(date);
  448. }
  449. /**
  450. * 增加 LocalDate ==> Date
  451. */
  452. public static Date toDate(LocalDate temporalAccessor)
  453. {
  454. LocalDateTime localDateTime = LocalDateTime.of(temporalAccessor, LocalTime.of(0, 0, 0));
  455. ZonedDateTime zdt = localDateTime.atZone(ZoneId.systemDefault());
  456. return Date.from(zdt.toInstant());
  457. }
  458. /**
  459. * 日期路径 即年/月/日 如2018/08/08
  460. */
  461. public static final String datePath()
  462. {
  463. Date now = new Date();
  464. return DateFormatUtils.format(now, "yyyy/MM/dd");
  465. }
  466. /**
  467. * 日期型字符串转化为日期 格式
  468. */
  469. public static Date parseDate(Object str)
  470. {
  471. if (str == null)
  472. {
  473. return null;
  474. }
  475. try
  476. {
  477. return parseDate(str.toString(), parsePatterns);
  478. }
  479. catch (ParseException e)
  480. {
  481. return null;
  482. }
  483. }
  484. /**
  485. * 计算距离今天天数
  486. * @param dates
  487. * @return
  488. */
  489. public static Integer sumDays(String dates) {
  490. Date date = StringToDate(dates, AFTConstants.YYYYMMDDHHMMSS);
  491. Date now=new Date();
  492. int days=(int)((now.getTime()-date.getTime())/(1000*3600*24));
  493. return days;
  494. }
  495. /**
  496. * 获取当前时间
  497. * @return
  498. */
  499. public static LocalDate getToDay() {
  500. return LocalDate.now();
  501. }
  502. public static LocalDate gettomorrow() {
  503. LocalDate today = LocalDate.now();
  504. return today.plusDays(1);
  505. }
  506. public static LocalDate getFirstDayOfThisWeek() {
  507. LocalDate today = LocalDate.now();
  508. return today.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
  509. }
  510. public static long getDaysBetween(Date startDate, Date endDate) {
  511. // 将Date转换为LocalDate(需指定时区)
  512. LocalDate start = startDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
  513. LocalDate end = endDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
  514. // 计算天数差(end - start)
  515. return ChronoUnit.DAYS.between(start, end);
  516. }
  517. public static void main(String[] args) {
  518. Date date = StringToDate("2023-08-08 09:00:00", AFTConstants.YYYYMMDDHHMMSS);
  519. Date date1 = StringToDate("2023-08-12 06:00:00", AFTConstants.YYYYMMDDHHMMSS);
  520. long daysBetween = getDaysBetween(date, date1);
  521. daysBetween+=1;
  522. System.out.println(daysBetween);
  523. for (int i = 0; i <daysBetween; i++) {
  524. LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
  525. LocalDate localDate1 = localDate.plusDays(i);
  526. System.out.println(localDate1);
  527. }
  528. }
  529. }