DateUtils.java 12 KB

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