DateUtils.java 14 KB

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