DateUtils.java 12 KB

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