DateUtils.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. package me.zhengjie.appapi.util;
  2. import java.text.ParseException;
  3. import java.text.ParsePosition;
  4. import java.text.SimpleDateFormat;
  5. import java.util.Calendar;
  6. import java.util.Date;
  7. import java.util.GregorianCalendar;
  8. import org.apache.commons.lang.StringUtils;
  9. /**
  10. * 时间处理工具,负责处理常见的字符串操作,包括字符串相关的变换、判断与计算等。
  11. *
  12. * @author chenguang.li
  13. * @date 2016年9月14日上午10:55:09
  14. *
  15. * com.yeepay.credit.api.core.util
  16. */
  17. public class DateUtils {
  18. /**
  19. * 指定格式化格式:yyyyMMdd
  20. *
  21. * @see com.ailk.dazzle.util.type.DateUtils#date2String(Date, int)
  22. * @see com.ailk.dazzle.util.type.DateUtils#string2Date(Date, int)
  23. */
  24. public final static int FORMATTYPE_yyyyMMdd = 0;
  25. /**
  26. * 指定格式化格式:yyyy-MM-dd
  27. *
  28. * @see com.ailk.dazzle.util.type.DateUtils#date2String(Date, int)
  29. * @see com.ailk.dazzle.util.type.DateUtils#string2Date(Date, int)
  30. */
  31. public final static int FORMATTYPE_yyyy_MM_dd = 1;
  32. /**
  33. * 指定格式化格式:HHmmss
  34. *
  35. * @see com.ailk.dazzle.util.type.DateUtils#date2String(Date, int)
  36. * @see com.ailk.dazzle.util.type.DateUtils#string2Date(Date, int)
  37. */
  38. public final static int FORMATTYPE_HHmmss = 2;
  39. /**
  40. * 指定格式化格式:HH:mm:ss
  41. *
  42. * @see com.ailk.dazzle.util.type.DateUtils#date2String(Date, int)
  43. * @see com.ailk.dazzle.util.type.DateUtils#string2Date(Date, int)
  44. */
  45. public final static int FORMATTYPE_HH_mm_ss = 3;
  46. /**
  47. * 指定格式化格式:HHmmssSSS
  48. *
  49. * @see com.ailk.dazzle.util.type.DateUtils#date2String(Date, int)
  50. * @see com.ailk.dazzle.util.type.DateUtils#string2Date(Date, int)
  51. */
  52. public final static int FORMATTYPE_HHmmssSSS = 4;
  53. /**
  54. * 指定格式化格式:HH:mm:ss.SSS
  55. *
  56. * @see com.ailk.dazzle.util.type.DateUtils#date2String(Date, int)
  57. * @see com.ailk.dazzle.util.type.DateUtils#string2Date(Date, int)
  58. */
  59. public final static int FORMATTYPE_HH_mm_ss_SSS = 5;
  60. /**
  61. * 指定格式化格式:yyyyMMddHHmmss
  62. *
  63. * @see com.ailk.dazzle.util.type.DateUtils#date2String(Date, int)
  64. * @see com.ailk.dazzle.util.type.DateUtils#string2Date(Date, int)
  65. */
  66. public final static int FORMATTYPE_yyyyMMddHHmmss = 6;
  67. /**
  68. * 指定格式化格式:yyyy-MM-dd HH:mm:ss
  69. *
  70. * @see com.ailk.dazzle.util.type.DateUtils#date2String(Date, int)
  71. * @see com.ailk.dazzle.util.type.DateUtils#string2Date(Date, int)
  72. */
  73. public final static int FORMATTYPE_yyyy_MM_dd_HH_mm_ss = 7;
  74. /**
  75. * 指定格式化格式:yyyyMMddHHmmssSSS
  76. *
  77. * @see com.ailk.dazzle.util.type.DateUtils#date2String(Date, int)
  78. * @see com.ailk.dazzle.util.type.DateUtils#string2Date(Date, int)
  79. */
  80. public final static int FORMATTYPE_yyyyMMddHHmmssSSS = 8;
  81. /**
  82. * 指定格式化格式:yyyy-MM-dd HH:mm:ss.SSS
  83. *
  84. * @see com.ailk.dazzle.util.type.DateUtils#date2String(Date, int)
  85. * @see com.ailk.dazzle.util.type.DateUtils#string2Date(Date, int)
  86. */
  87. public final static int FORMATTYPE_yyyy_MM_dd_HH_mm_ss_SSS = 9;
  88. /**
  89. * 指定格式化格式:yyyyMMddHHmmss.SSS
  90. *
  91. * @see com.ailk.dazzle.util.type.DateUtils#date2String(Date, int)
  92. * @see com.ailk.dazzle.util.type.DateUtils#string2Date(Date, int)
  93. */
  94. public final static int FORMATTYPE_HHmmss_SSS = 10;
  95. /**
  96. * 将Date对象转换为日期字符串。
  97. *
  98. * @param date 日期对象
  99. * @param formatType 指定格式:
  100. * <ul>
  101. * <li>0-yyyyMMdd</li>
  102. * <li>1-yyyy-MM-dd</li>
  103. * <li>2-HHmmss</li>
  104. * <li>3-HH:mm:ss</li>
  105. * <li>4-HHmmssSSS</li>
  106. * <li>5-HH:mm:ss.SSS</li>
  107. * <li>6-yyyyMMddHHmmss</li>
  108. * <li>7-yyyy-MM-dd HH:mm:ss</li>
  109. * <li>8-yyyyMMddHHmmssSSS</li>
  110. * <li>9-yyyy-MM-dd HH:mm:ss.SSS</li>
  111. * <li>10-yyyy-MM-dd HH:mm:ss.SSS</li>
  112. * </ul>
  113. * @return 日期字符串
  114. * @author miaofc
  115. * @date Nov 22, 2011 5:30:32 PM
  116. */
  117. public static String date2String(Date date, int formatType) {
  118. // 如果日期为null,则直接返回空值
  119. if (date == null) {
  120. return "";
  121. }
  122. // 格式化字符串数组
  123. String[] patterns = { "yyyyMMdd", "yyyy-MM-dd", "HHmmss", "HH:mm:ss", "HHmmssSSS", "HH:mm:ss.SSS",
  124. "yyyyMMddHHmmss", "yyyy-MM-dd HH:mm:ss", "yyyyMMddHHmmssSSS", "yyyy-MM-dd HH:mm:ss.SSS", "HHmmss.SSS" };
  125. // 根据选择类型格式化字符串
  126. SimpleDateFormat format = new SimpleDateFormat(patterns[formatType]);
  127. return format.format(date);
  128. }
  129. /**
  130. * 将日期字符串转化为Date对象。
  131. *
  132. * @param dateStr
  133. * @param formatType 指定格式:
  134. * <ul>
  135. * <li>0-yyyyMMdd</li>
  136. * <li>1-yyyy-MM-dd</li>
  137. * <li>2-HHmmss</li>
  138. * <li>3-HH:mm:ss</li>
  139. * <li>4-HHmmssSSS</li>
  140. * <li>5-HH:mm:ss.SSS</li>
  141. * <li>6-yyyyMMddHHmmss</li>
  142. * <li>7-yyyy-MM-dd HH:mm:ss</li>
  143. * <li>8-yyyyMMddHHmmssSSS</li>
  144. * <li>9-yyyy-MM-dd HH:mm:ss.SSS</li>
  145. * </ul>
  146. * @return Date对象
  147. * @throws ParseException
  148. * @author miaofc
  149. * @date Nov 22, 2011 5:35:27 PM
  150. */
  151. public static Date string2Date(String dateStr, int formatType) throws ParseException {
  152. // 如果字符串为null,则直接返回null
  153. if (StringUtils.isBlank(dateStr)) {
  154. return null;
  155. }
  156. // 格式化字符串数组
  157. String[] patterns = { "yyyyMMdd", "yyyy-MM-dd", "HHmmss", "HH:mm:ss", "HHmmssSSS", "HH:mm:ss.SSS",
  158. "yyyyMMddHHmmss", "yyyy-MM-dd HH:mm:ss", "yyyyMMddHHmmssSSS", "yyyy-MM-dd HH:mm:ss.SSS" };
  159. // 根据选择类型解析字符串
  160. SimpleDateFormat format = new SimpleDateFormat(patterns[formatType]);
  161. return format.parse(dateStr);
  162. }
  163. /**
  164. * 得到一年中的某一天
  165. *
  166. * @param year 年份
  167. * @param ordinal 第几天,计数从1开始
  168. * @return 一年中第ordinal天的Date对象
  169. * @author miaofc
  170. * @throws ParseException
  171. * @date Nov 24, 2011 12:22:57 PM
  172. */
  173. public static Date getDateOfDayInYear(int year, int ordinal) throws ParseException {
  174. // 计算出该年份第一天
  175. SimpleDateFormat format = new SimpleDateFormat("yyyy");
  176. Date firstDay = format.parse(String.valueOf(year));
  177. // 计算出目标日
  178. return new Date(firstDay.getTime() + 86400000 * (ordinal - 1));
  179. }
  180. /**
  181. * 得到季度中的某一天
  182. *
  183. * @param year 年份
  184. * @param season 季度,计数从1开始
  185. * @param ordinal 第几天,计数从1开始
  186. * @return 该年份该季度下的第ordinal天的Date对象
  187. * @author miaofc
  188. * @throws ParseException
  189. * @date Nov 24, 2011 12:25:12 PM
  190. */
  191. public static Date getDateOfDayInSeason(int year, int season, int ordinal) throws ParseException {
  192. // 根据季度,得到季度的起始月份
  193. String firstMonth = String.valueOf(year);
  194. switch (season) {
  195. case 1:
  196. firstMonth += "01";
  197. break;
  198. case 2:
  199. firstMonth += "04";
  200. break;
  201. case 3:
  202. firstMonth += "07";
  203. break;
  204. case 4:
  205. firstMonth += "10";
  206. break;
  207. // 非法的季度数
  208. default:
  209. throw new IllegalArgumentException("IllegalSeasonNumber");
  210. }
  211. // 计算出该季度第一天
  212. SimpleDateFormat format = new SimpleDateFormat("yyyyMM");
  213. Date firstDay = format.parse(firstMonth);
  214. // 计算出目标日
  215. return new Date(firstDay.getTime() + 86400000 * (ordinal - 1));
  216. }
  217. /**
  218. * 得到月份中的某一天
  219. *
  220. * @param year 年份
  221. * @param month 月份,计数从1开始
  222. * @param ordinal 第几天,计数从1开始
  223. * @return 该年份该月份下的第ordinal天的Date对象
  224. * @throws ParseException
  225. * @author miaofch
  226. * @date Feb 8, 2012 3:39:11 PM
  227. */
  228. public static Date getDateOfDayInMonth(int year, int month, int ordinal) throws ParseException {
  229. String yearStr = String.valueOf(year);
  230. String monthStr = month > 10 ? String.valueOf(month) : "0" + String.valueOf(month);
  231. // 计算出该月份第一天
  232. SimpleDateFormat format = new SimpleDateFormat("yyyyMM");
  233. Date firstDay = format.parse(yearStr + monthStr);
  234. // 计算出目标日
  235. return new Date(firstDay.getTime() + 86400000 * (ordinal - 1));
  236. }
  237. /**
  238. * 得到一周中的某一天
  239. *
  240. * @param someDayInWeek 周内的任意一天
  241. * @param ordinal 希望得到第几天。计数从1开始,每周从周日开始。
  242. * @return 该周内的第ordinal天的Date对象
  243. * @author miaofc
  244. * @date Nov 24, 2011 12:28:07 PM
  245. */
  246. public static Date getDateOfDayInWeek(Date someDayInWeek, int ordinal) {
  247. Calendar someDayInWeekC = new GregorianCalendar();
  248. someDayInWeekC.setTime(someDayInWeek);
  249. // 计算出当周的首日
  250. Calendar firstDay = new GregorianCalendar();
  251. firstDay.set(someDayInWeekC.get(Calendar.YEAR), someDayInWeekC.get(Calendar.MONTH),
  252. someDayInWeekC.get(Calendar.DATE));
  253. firstDay.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
  254. // 计算出目标日
  255. return new Date(firstDay.getTimeInMillis() + 86400000 * (ordinal - 1));
  256. }
  257. /**
  258. * 求日期是一年中的第几天
  259. *
  260. * @param day 待判断的日期
  261. * @return 目标日是第几天。计数从1开始
  262. * @author miaofc
  263. * @date Nov 25, 2011 2:41:30 PM
  264. */
  265. public static int getOrinalOfDayInYear(Date day) {
  266. Calendar c = new GregorianCalendar();
  267. c.setTime(day);
  268. return c.get(Calendar.DAY_OF_YEAR);
  269. }
  270. /**
  271. * 求日期是一个季度中的第几天
  272. *
  273. * @param day 待判断的日期
  274. * @return 目标日是第几天。计数从1开始
  275. * @author miaofch
  276. * @date Feb 16, 2012 10:38:32 AM
  277. */
  278. public static int getOrinalOfDayInSeason(Date day) {
  279. Calendar c = new GregorianCalendar();
  280. c.setTime(day);
  281. // 得到月份,计数从1开始
  282. int month = c.get(Calendar.MONTH) + 1;
  283. // 得到年份,计数从1开始
  284. int year = c.get(Calendar.YEAR);
  285. // 根据季度的不同,区分计算
  286. int orinal = 0;
  287. // 第一季度
  288. if (1 <= month && month <= 3) {
  289. for (int i = 1; i < month; i++) {
  290. orinal += getNumberOfDaysInMonth(year, i);
  291. }
  292. orinal += getOrinalOfDayInMonth(day);
  293. }
  294. // 第二季度
  295. else if (4 <= month && month <= 6) {
  296. for (int i = 4; i < month; i++) {
  297. orinal += getNumberOfDaysInMonth(year, i);
  298. }
  299. orinal += getOrinalOfDayInMonth(day);
  300. }
  301. // 第三季度
  302. else if (7 <= month && month <= 9) {
  303. for (int i = 7; i < month; i++) {
  304. orinal += getNumberOfDaysInMonth(year, i);
  305. }
  306. orinal += getOrinalOfDayInMonth(day);
  307. }
  308. // 第四季度
  309. else if (10 <= month && month <= 12) {
  310. for (int i = 10; i < month; i++) {
  311. orinal += getNumberOfDaysInMonth(year, i);
  312. }
  313. orinal += getOrinalOfDayInMonth(day);
  314. }
  315. return orinal;
  316. }
  317. /**
  318. * 求日期是一个月中的第几天
  319. *
  320. * @param day 待判断的日期
  321. * @return 目标日是第几天。计数从1开始
  322. * @author miaofc
  323. * @date Nov 25, 2011 2:41:46 PM
  324. */
  325. public static int getOrinalOfDayInMonth(Date day) {
  326. Calendar c = new GregorianCalendar();
  327. c.setTime(day);
  328. return c.get(Calendar.DAY_OF_MONTH);
  329. }
  330. /**
  331. * 求日期是一周中的第几天
  332. *
  333. * @param day 待判断的日期
  334. * @return 目标日是第几天。计数从1开始
  335. * @author miaofc
  336. * @date Nov 25, 2011 2:42:15 PM
  337. */
  338. public static int getOrinalOfDayInWeek(Date day) {
  339. Calendar c = new GregorianCalendar();
  340. c.setTime(day);
  341. return c.get(Calendar.DAY_OF_WEEK);
  342. }
  343. /**
  344. * 求某一年一共有多少天
  345. *
  346. * @param year 年份
  347. * @return 年内一共有多少天
  348. * @author miaofc
  349. * @date Nov 25, 2011 2:43:23 PM
  350. */
  351. public static int getNumberOfDaysInYear(int year) {
  352. // 若该年份为闰年,返回366
  353. if ((year % 100 != 0 && year % 4 == 0) || (year % 400 == 0)) {
  354. return 366;
  355. }
  356. // 普通年份返回365
  357. return 365;
  358. }
  359. /**
  360. * 得到某个季度一共有多少天
  361. *
  362. * @param someDayInSeanson 季度内的某一天
  363. * @return 季度内一共有多少天
  364. * @author miaofch
  365. * @date Feb 17, 2012 5:37:09 PM
  366. */
  367. public static int getNumberOfDaysInSeason(Date someDayInSeanson) {
  368. // 计算日期的所在年和季度
  369. Calendar c = new GregorianCalendar();
  370. c.setTime(someDayInSeanson);
  371. int year = c.get(Calendar.YEAR);
  372. int season = c.get(Calendar.MONTH) / 3 + 1;
  373. // 计算季度内共有几天
  374. return getNumberOfDaysInSeason(year, season);
  375. }
  376. /**
  377. * 得到某个季度一共有多少天
  378. *
  379. * @param year 年份
  380. * @param season 季度。计数从1开始
  381. * @return 季度内一共有多少天
  382. * @author miaofch
  383. * @date Feb 16, 2012 11:12:06 AM
  384. */
  385. public static int getNumberOfDaysInSeason(int year, int season) {
  386. // 求季度内的第一个月
  387. int firstMonth = 0;
  388. switch (season) {
  389. case 1:
  390. firstMonth = 1;
  391. break;
  392. case 2:
  393. firstMonth = 4;
  394. break;
  395. case 3:
  396. firstMonth = 7;
  397. break;
  398. case 4:
  399. firstMonth = 10;
  400. break;
  401. // 非法的季度数
  402. default:
  403. throw new IllegalArgumentException("IllegalSeasonNumber");
  404. }
  405. // 计算日期总数
  406. int number = 0;
  407. for (int i = 0; i < 3; i++) {
  408. number += getNumberOfDaysInMonth(year, firstMonth + i);
  409. }
  410. return number;
  411. }
  412. /**
  413. * 求某月一共有多少天
  414. *
  415. * @param year 年份
  416. * @param month 月份。从1开始
  417. * @return 该月中一共有多少天
  418. * @author miaofc
  419. * @date Nov 25, 2011 2:43:36 PM
  420. */
  421. public static int getNumberOfDaysInMonth(int year, int month) {
  422. // 根据日期设置日历实体
  423. Calendar c = new GregorianCalendar();
  424. // 日历实体中对于月份的计算从0开始
  425. c.set(year, month - 1, 1);
  426. int numberOfDays = c.getActualMaximum(Calendar.DATE);
  427. return numberOfDays;
  428. }
  429. /**
  430. * 求当前年份一共有几天
  431. *
  432. * @return 当前年份一共有多少天
  433. * @author miaofc
  434. * @date Nov 25, 2011 2:49:44 PM
  435. */
  436. public static int getNumberOfDaysInCurrentYear() {
  437. // 设置日历实体,求当前年份
  438. Calendar c = new GregorianCalendar();
  439. c.setTimeInMillis(System.currentTimeMillis());
  440. int year = c.get(Calendar.YEAR);
  441. // 根据年份求天数
  442. return getNumberOfDaysInYear(year);
  443. }
  444. /**
  445. * 求当前月份一共有几天
  446. *
  447. * @return 当前月份一共有多少天
  448. * @author miaofc
  449. * @date Nov 25, 2011 2:49:27 PM
  450. */
  451. public static int getNumberOfDaysInCurrentMonth() {
  452. // 设置日历实体
  453. Calendar c = new GregorianCalendar();
  454. c.setTimeInMillis(System.currentTimeMillis());
  455. int numberOfDays = c.getActualMaximum(Calendar.DATE);
  456. return numberOfDays;
  457. }
  458. /**
  459. * 取得date在域field上偏移amount后的值
  460. *
  461. * @param date 日期
  462. * @param field 要偏移的时间域,如Calendar.YEAR、Calendar.MONTH等
  463. * @param amount 偏移量
  464. * @return 偏移后的日期时间
  465. * @throws Exception
  466. * @author miaofc
  467. * @date Nov 22, 2011 5:30:32 PM
  468. */
  469. public static Date dateOffset(Date date, int field, int amount) throws Exception {
  470. Calendar c = new GregorianCalendar();
  471. c.setTime(date);
  472. c.add(field, amount);
  473. return c.getTime();
  474. }
  475. /**
  476. * 得到两个时间相差的毫秒数
  477. *
  478. * @param date1 时间1
  479. * @param date2 时间2
  480. * @return 两个时间之间相差的毫秒数。计算顺序为date1-date2
  481. * @author miaofc
  482. * @date Nov 24, 2011 1:10:03 PM
  483. */
  484. public static long getDiffMillis(Date date1, Date date2) {
  485. return date1.getTime() - date2.getTime();
  486. }
  487. /**
  488. * 得到两个时间相差的秒数
  489. *
  490. * @param date1 时间1
  491. * @param date2 时间2
  492. * @return 两个时间之间相差的秒数。计算顺序为date1-date2
  493. * @author miaofc
  494. * @date Nov 24, 2011 1:10:18 PM
  495. */
  496. public static long getDiffSeconds(Date date1, Date date2) {
  497. return getDiffMillis(date1, date2) / 1000;
  498. }
  499. /**
  500. * 得到两个时间相差的分钟数
  501. *
  502. * @param date1 时间1
  503. * @param date2 时间2
  504. * @return 两个时间之间相差的分钟数。计算顺序为date1-date2
  505. * @author miaofc
  506. * @date Nov 24, 2011 1:10:29 PM
  507. */
  508. public static long getDiffMinutes(Date date1, Date date2) {
  509. return getDiffSeconds(date1, date2) / 60;
  510. }
  511. /**
  512. * 得到两个时间相差的小时数
  513. *
  514. * @param date1 时间1
  515. * @param date2 时间2
  516. * @return 两个时间之间相差的小时数。计算顺序为date1-date2
  517. * @author miaofc
  518. * @date Nov 24, 2011 1:10:44 PM
  519. */
  520. public static long getDiffHours(Date date1, Date date2) {
  521. return getDiffMinutes(date1, date2) / 60;
  522. }
  523. /**
  524. * 得到两个时间相差的天数
  525. *
  526. * @param date1 时间1
  527. * @param date2 时间2
  528. * @return 两个时间之间相差的天数。计算顺序为date1-date2
  529. * @author miaofc
  530. * @date Nov 24, 2011 1:11:01 PM
  531. */
  532. public static long getDiffDays(Date date1, Date date2) {
  533. return getDiffHours(date1, date2) / 24;
  534. }
  535. /**
  536. * 得到两个时间相差的月数,每月天数认为是30天。
  537. *
  538. * @param date1 时间1
  539. * @param date2 时间2
  540. * @return 两个时间之间相差的月数。计算顺序为date1-date2
  541. * @author miaofc
  542. * @date Nov 24, 2011 1:11:15 PM
  543. */
  544. public static long getDiffMonths(Date date1, Date date2) {
  545. return getDiffDays(date1, date2) / 30;
  546. }
  547. /**
  548. * 得到两个时间相差的自然月数
  549. *
  550. * @param date1 时间1
  551. * @param date2 时间2
  552. * @return 两个时间之间相差的自然月数。计算顺序为date1-date2
  553. * @author miaofc
  554. * @date Nov 28, 2011 2:43:34 PM
  555. */
  556. public static int getDiffNaturalMonth(Date date1, Date date2) {
  557. Calendar calendar1 = new GregorianCalendar();
  558. calendar1.setTime(date1);
  559. Calendar calendar2 = new GregorianCalendar();
  560. calendar2.setTime(date2);
  561. int mouth1 = calendar1.get(Calendar.MONTH) + 1;
  562. int mouth2 = calendar2.get(Calendar.MONTH) + 1;
  563. return getDiffYears(date1, date2) * 12 - (mouth2 - mouth1);
  564. }
  565. /**
  566. * 得到两个时间相差的年数
  567. *
  568. * @param date1 时间1
  569. * @param date2 时间2
  570. * @return 两个时间之间相差的年数。计算顺序为date1-date2
  571. * @author miaofc
  572. * @date Nov 24, 2011 1:11:27 PM
  573. */
  574. public static int getDiffYears(Date date1, Date date2) {
  575. Calendar calendar1 = new GregorianCalendar();
  576. calendar1.setTime(date1);
  577. Calendar calendar2 = new GregorianCalendar();
  578. calendar2.setTime(date2);
  579. int year1 = calendar1.get(Calendar.YEAR);
  580. int year2 = calendar2.get(Calendar.YEAR);
  581. return year1 - year2;
  582. }
  583. public static void main(String[] args) throws ParseException {
  584. //System.out.println(date2String(string2Date("101010", 2), 9));
  585. // Date d2 =DateFormatUtils.timeStrToDate("2021-01-31");
  586. // Date d=DateFormatUtils.timeStrToDate("2022-01-19");
  587. // System.out.println(getDiffNaturalMonth(d, d2)); ;
  588. // System.out.println(getDiffMonths(d, d2)); ;
  589. System.out.println(getNo());
  590. }
  591. public static Date getString2Date(String dateformat) {
  592. SimpleDateFormat sdf = new SimpleDateFormat(dateformat);
  593. ParsePosition pos = new ParsePosition(0);
  594. Date date= new Date();
  595. String dateStr = sdf.format(date);
  596. return sdf.parse(dateStr, pos);
  597. }
  598. public static String getNo() {
  599. SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
  600. Date currentTime = new Date();
  601. String businessNo = sdf.format(currentTime);
  602. //String businessNo = str + (int) (Math.random() * 1000.0D);
  603. return businessNo;
  604. }
  605. }