DateUtils.java 13 KB

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