DateUtils.java 12 KB

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