DateUtil.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. package com.yihu.wlyy.statistics.util;
  2. import org.apache.commons.lang3.StringUtils;
  3. import java.text.ParsePosition;
  4. import java.text.SimpleDateFormat;
  5. import java.util.*;
  6. public class DateUtil {
  7. public static final String HH_MM = "HH:mm";
  8. public static final String HH_MM_SS = "HH:mm:ss";
  9. public static final String YY = "yy";
  10. public static final String YYYYMM = "yyyyMM";
  11. public static final String YYYYMMDD = "yyyyMMdd";
  12. public static final String YYYY_MM_DD = "yyyy-MM-dd";
  13. public static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
  14. public static final String YYYY_MM_DD_HH = "yyyy-MM-dd HH";
  15. public static final String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
  16. public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
  17. /**
  18. * 字符串转时间格式
  19. */
  20. public static Date strToDate(String strDate) {
  21. if (StringUtils.isEmpty(strDate)) {
  22. return null;
  23. }
  24. else{
  25. int length = strDate.length();
  26. if(strDate.contains("-"))
  27. {
  28. if(length == 10)
  29. {
  30. return strToDate(strDate,YYYY_MM_DD);
  31. }
  32. else if(length == 19)
  33. {
  34. return strToDate(strDate,YYYY_MM_DD_HH_MM_SS);
  35. }
  36. }
  37. else{
  38. if(length == 8)
  39. {
  40. return strToDate(strDate,YYYYMMDD);
  41. }
  42. else if(length == 14)
  43. {
  44. return strToDate(strDate,YYYYMMDDHHMMSS);
  45. }
  46. }
  47. }
  48. return null;
  49. }
  50. /**
  51. * 获取现在时间
  52. *
  53. * @return 返回时间类型 yyyy-MM-dd HH:mm:ss
  54. */
  55. public static Date getNowDate() {
  56. Date currentTime = new Date();
  57. SimpleDateFormat formatter = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
  58. String dateString = formatter.format(currentTime);
  59. ParsePosition pos = new ParsePosition(0);
  60. return formatter.parse(dateString, pos);
  61. }
  62. /**
  63. * 获取现在时间
  64. *
  65. * @return返回短时间格式 yyyy-MM-dd
  66. */
  67. public static Date getNowDateShort() {
  68. Date currentTime = new Date();
  69. SimpleDateFormat formatter = new SimpleDateFormat(YYYY_MM_DD);
  70. String dateString = formatter.format(currentTime);
  71. return strToDate(dateString, YYYY_MM_DD);
  72. }
  73. /**
  74. * 获取现在时间
  75. *
  76. * @return返回字符串格式 yyyy-MM-dd HH:mm:ss
  77. */
  78. public static String getStringDate() {
  79. Date currentTime = new Date();
  80. SimpleDateFormat formatter = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
  81. return formatter.format(currentTime);
  82. }
  83. /**
  84. * 获取现在时间
  85. *
  86. * @return返回字符串格式 yyyy-MM-dd HH:mm:ss
  87. */
  88. public static String getStringDate(String format) {
  89. Date currentTime = new Date();
  90. SimpleDateFormat formatter = new SimpleDateFormat(format);
  91. return formatter.format(currentTime);
  92. }
  93. /**
  94. * 获取现在时间
  95. *
  96. * @return 返回短时间字符串格式yyyy-MM-dd
  97. */
  98. public static String getStringDateShort() {
  99. Date currentTime = new Date();
  100. SimpleDateFormat formatter = new SimpleDateFormat(YYYY_MM_DD);
  101. return formatter.format(currentTime);
  102. }
  103. /**
  104. * 获取时间 小时:分;秒 HH:mm:ss
  105. *
  106. * @return
  107. */
  108. public static String getTimeShort() {
  109. SimpleDateFormat formatter = new SimpleDateFormat(HH_MM_SS);
  110. Date currentTime = new Date();
  111. return formatter.format(currentTime);
  112. }
  113. /**
  114. * 将长时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss
  115. *
  116. * @param strDate
  117. * @return
  118. */
  119. public static Date strToDateLong(String strDate) {
  120. if (StringUtils.isEmpty(strDate)) {
  121. return null;
  122. }
  123. SimpleDateFormat formatter = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
  124. ParsePosition pos = new ParsePosition(0);
  125. return formatter.parse(strDate, pos);
  126. }
  127. public static Date strToDateShort(String strDate) {
  128. if (StringUtils.isEmpty(strDate)) {
  129. return null;
  130. }
  131. SimpleDateFormat formatter = new SimpleDateFormat(YYYY_MM_DD);
  132. ParsePosition pos = new ParsePosition(0);
  133. return formatter.parse(strDate, pos);
  134. }
  135. /**
  136. * 将长时间格式时间转换为字符串 yyyy-MM-dd HH:mm:ss
  137. *
  138. * @param dateDate
  139. * @return
  140. */
  141. public static String dateToStrLong(Date dateDate) {
  142. if (dateDate == null) {
  143. return "";
  144. }
  145. SimpleDateFormat formatter = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
  146. return formatter.format(dateDate);
  147. }
  148. /**
  149. * 将长时间格式时间转换为字符串 yyyy-MM-dd
  150. *
  151. * @param dateDate
  152. * @return
  153. */
  154. public static String dateToStrShort(Date dateDate) {
  155. if (dateDate == null) {
  156. return "";
  157. }
  158. SimpleDateFormat formatter = new SimpleDateFormat(YYYY_MM_DD);
  159. return formatter.format(dateDate);
  160. }
  161. /**
  162. * 将短时间格式时间转换为字符串 yyyy-MM-dd
  163. *
  164. * @param dateDate
  165. * @param k
  166. * @return
  167. */
  168. public static String dateToStr(Date dateDate, String format) {
  169. if (dateDate == null) {
  170. return "";
  171. }
  172. SimpleDateFormat formatter = new SimpleDateFormat(format);
  173. return formatter.format(dateDate);
  174. }
  175. /**
  176. * 将短时间格式字符串转换为时间
  177. *
  178. * @param strDate
  179. * @return
  180. */
  181. public static Date strToDate(String strDate, String format) {
  182. if (StringUtils.isEmpty(strDate)) {
  183. return null;
  184. }
  185. SimpleDateFormat formatter = new SimpleDateFormat(format);
  186. ParsePosition pos = new ParsePosition(0);
  187. return formatter.parse(strDate, pos);
  188. }
  189. public static Date strToDateAppendNowTime(String strDate, String format) {
  190. if (StringUtils.isEmpty(strDate)) {
  191. return null;
  192. }
  193. strDate += " " + getTimeShort();
  194. SimpleDateFormat formatter = new SimpleDateFormat(format);
  195. ParsePosition pos = new ParsePosition(0);
  196. return formatter.parse(strDate, pos);
  197. }
  198. /**
  199. * 得到现在时间
  200. *
  201. * @return
  202. */
  203. public static Date getNow() {
  204. Date currentTime = new Date();
  205. return currentTime;
  206. }
  207. /**
  208. * 提取一个月中的最后一天
  209. *
  210. * @param day
  211. * @return
  212. */
  213. public static Date getLastDate(long day) {
  214. Date date = new Date();
  215. long date_3_hm = date.getTime() - 3600000 * 34 * day;
  216. Date date_3_hm_date = new Date(date_3_hm);
  217. return date_3_hm_date;
  218. }
  219. /**
  220. * 得到现在时间
  221. *
  222. * @return 字符串 yyyyMMdd HHmmss
  223. */
  224. public static String getStringToday() {
  225. Date currentTime = new Date();
  226. SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd HHmmss");
  227. String dateString = formatter.format(currentTime);
  228. return dateString;
  229. }
  230. /**
  231. * 得到现在小时
  232. */
  233. public static String getHour() {
  234. Date currentTime = new Date();
  235. SimpleDateFormat formatter = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
  236. String dateString = formatter.format(currentTime);
  237. String hour;
  238. hour = dateString.substring(11, 13);
  239. return hour;
  240. }
  241. /**
  242. * 得到现在分钟
  243. *
  244. * @return
  245. */
  246. public static String getTime() {
  247. Date currentTime = new Date();
  248. SimpleDateFormat formatter = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
  249. String dateString = formatter.format(currentTime);
  250. String min;
  251. min = dateString.substring(14, 16);
  252. return min;
  253. }
  254. /**
  255. * 根据用户传入的时间表示格式,返回当前时间的格式 如果是yyyyMMdd,注意字母y不能大写。
  256. *
  257. * @param sformat
  258. * yyyyMMddhhmmss
  259. * @return
  260. */
  261. public static String getUserDate(String sformat) {
  262. Date currentTime = new Date();
  263. SimpleDateFormat formatter = new SimpleDateFormat(sformat);
  264. String dateString = formatter.format(currentTime);
  265. return dateString;
  266. }
  267. /**
  268. * 二个小时时间间的差值,必须保证二个时间都是"HH:MM"的格式,返回字符型的分钟
  269. */
  270. public static String getTwoHour(String st1, String st2) {
  271. String[] kk = null;
  272. String[] jj = null;
  273. kk = st1.split(":");
  274. jj = st2.split(":");
  275. if (Integer.parseInt(kk[0]) < Integer.parseInt(jj[0]))
  276. return "0";
  277. else {
  278. double y = Double.parseDouble(kk[0]) + Double.parseDouble(kk[1]) / 60;
  279. double u = Double.parseDouble(jj[0]) + Double.parseDouble(jj[1]) / 60;
  280. if ((y - u) > 0)
  281. return y - u + "";
  282. else
  283. return "0";
  284. }
  285. }
  286. /**
  287. * 得到二个日期间的间隔天数
  288. */
  289. public static String getTwoDay(String sj1, String sj2) {
  290. SimpleDateFormat myFormatter = new SimpleDateFormat(YYYY_MM_DD);
  291. long day = 0;
  292. try {
  293. Date date = myFormatter.parse(sj1);
  294. Date mydate = myFormatter.parse(sj2);
  295. day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
  296. } catch (Exception e) {
  297. return "";
  298. }
  299. return day + "";
  300. }
  301. /**
  302. * 时间前推或后推分钟,其中JJ表示分钟.
  303. */
  304. public static String getPreTime(String sj1, String jj) {
  305. SimpleDateFormat format = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
  306. String mydate1 = "";
  307. try {
  308. Date date1 = format.parse(sj1);
  309. long Time = (date1.getTime() / 1000) + Integer.parseInt(jj) * 60;
  310. date1.setTime(Time * 1000);
  311. mydate1 = format.format(date1);
  312. } catch (Exception e) {
  313. }
  314. return mydate1;
  315. }
  316. /**
  317. * 得到一个时间延后或前移几分钟的时间,nowdate为时间,delay为前移或后延的分钟数
  318. */
  319. public static Date getNextMin(Date date, int delay) {
  320. try {
  321. Calendar cal = Calendar.getInstance();
  322. cal.setTime(date);
  323. cal.add(Calendar.MINUTE, delay);
  324. return cal.getTime();
  325. } catch (Exception e) {
  326. return null;
  327. }
  328. }
  329. /**
  330. * 得到一个时间延后或前移几天的时间,nowdate为时间,delay为前移或后延的天数
  331. */
  332. public static String getNextDay(String nowdate, int delay) {
  333. try {
  334. SimpleDateFormat format = new SimpleDateFormat(YYYY_MM_DD);
  335. String mdate = "";
  336. Date d = strToDate(nowdate, YYYY_MM_DD);
  337. long myTime = (d.getTime() / 1000) + delay * 24 * 60 * 60;
  338. d.setTime(myTime * 1000);
  339. mdate = format.format(d);
  340. return mdate;
  341. } catch (Exception e) {
  342. return "";
  343. }
  344. }
  345. // public static String getNextDay(Date d, int delay) {
  346. // try {
  347. // SimpleDateFormat format = new SimpleDateFormat(YYYY_MM_DD);
  348. // String mdate = "";
  349. // long myTime = (d.getTime() / 1000) + delay * 24 * 60 * 60;
  350. // d.setTime(myTime * 1000);
  351. // mdate = format.format(d);
  352. // return mdate;
  353. // } catch (Exception e) {
  354. // return "";
  355. // }
  356. // }
  357. public static String getNextDay(Date d, int days) {
  358. Calendar c = Calendar.getInstance();
  359. c.setTime(d);
  360. c.add(Calendar.DATE, days);
  361. return dateToStrShort(c.getTime());
  362. }
  363. public static String getNextMonth(Date d, int months) {
  364. Calendar c = Calendar.getInstance();
  365. c.setTime(d);
  366. c.add(Calendar.MONTH, months);
  367. return dateToStrShort(c.getTime());
  368. }
  369. public static String getNextYear(Date d, int year) {
  370. Calendar c = Calendar.getInstance();
  371. c.setTime(d);
  372. c.add(Calendar.YEAR, year);
  373. return dateToStrShort(c.getTime());
  374. }
  375. /**
  376. * 判断是否润年
  377. *
  378. * @param ddate
  379. * @return
  380. */
  381. public static boolean isLeapYear(String ddate) {
  382. /**
  383. * 详细设计: 1.被400整除是闰年,否则: 2.不能被4整除则不是闰年 3.能被4整除同时不能被100整除则是闰年
  384. * 3.能被4整除同时能被100整除则不是闰年
  385. */
  386. Date d = strToDate(ddate, YYYY_MM_DD);
  387. GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
  388. gc.setTime(d);
  389. int year = gc.get(Calendar.YEAR);
  390. if ((year % 400) == 0)
  391. return true;
  392. else if ((year % 4) == 0) {
  393. if ((year % 100) == 0)
  394. return false;
  395. else
  396. return true;
  397. } else
  398. return false;
  399. }
  400. /**
  401. * 返回美国时间格式 26 Apr 2006
  402. *
  403. * @param str
  404. * @return
  405. */
  406. public static String getEDate(String str) {
  407. SimpleDateFormat formatter = new SimpleDateFormat(YYYY_MM_DD);
  408. ParsePosition pos = new ParsePosition(0);
  409. Date strtodate = formatter.parse(str, pos);
  410. String j = strtodate.toString();
  411. String[] k = j.split(" ");
  412. return k[2] + k[1].toUpperCase() + k[5].substring(2, 4);
  413. }
  414. /**
  415. * 获取一个月的最后一天
  416. *
  417. * @param dat
  418. * @return
  419. */
  420. public static String getEndDateOfMonth(String dat) {// yyyy-MM-dd
  421. String str = dat.substring(0, 8);
  422. String month = dat.substring(5, 7);
  423. int mon = Integer.parseInt(month);
  424. if (mon == 1 || mon == 3 || mon == 5 || mon == 7 || mon == 8 || mon == 10 || mon == 12) {
  425. str += "31";
  426. } else if (mon == 4 || mon == 6 || mon == 9 || mon == 11) {
  427. str += "30";
  428. } else {
  429. if (isLeapYear(dat)) {
  430. str += "29";
  431. } else {
  432. str += "28";
  433. }
  434. }
  435. return str;
  436. }
  437. /**
  438. * 判断二个时间是否在同一个周
  439. *
  440. * @param date1
  441. * @param date2
  442. * @return
  443. */
  444. public static boolean isSameWeekDates(Date date1, Date date2) {
  445. Calendar cal1 = Calendar.getInstance();
  446. Calendar cal2 = Calendar.getInstance();
  447. cal1.setTime(date1);
  448. cal2.setTime(date2);
  449. int subYear = cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR);
  450. if (0 == subYear) {
  451. if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
  452. return true;
  453. } else if (1 == subYear && 11 == cal2.get(Calendar.MONTH)) {
  454. // 如果12月的最后一周横跨来年第一周的话则最后一周即算做来年的第一周
  455. if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
  456. return true;
  457. } else if (-1 == subYear && 11 == cal1.get(Calendar.MONTH)) {
  458. if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
  459. return true;
  460. }
  461. return false;
  462. }
  463. /**
  464. * 产生周序列,即得到当前时间所在的年度是第几周
  465. *
  466. * @return
  467. */
  468. public static String getSeqWeek() {
  469. Calendar c = Calendar.getInstance(Locale.CHINA);
  470. String week = Integer.toString(c.get(Calendar.WEEK_OF_YEAR));
  471. if (week.length() == 1)
  472. week = "0" + week;
  473. String year = Integer.toString(c.get(Calendar.YEAR));
  474. return year + week;
  475. }
  476. /**
  477. * 获得一个日期所在的周的星期几的日期,如要找出2002年2月3日所在周的星期一是几号
  478. *
  479. * @param sdate
  480. * @param num
  481. * @return
  482. */
  483. public static String getWeek(String sdate, String num) {
  484. // 再转换为时间
  485. Date dd = DateUtil.strToDate(sdate, YYYY_MM_DD);
  486. Calendar c = Calendar.getInstance();
  487. c.setTime(dd);
  488. if (num.equals("1")) // 返回星期一所在的日期
  489. c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
  490. else if (num.equals("2")) // 返回星期二所在的日期
  491. c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
  492. else if (num.equals("3")) // 返回星期三所在的日期
  493. c.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);
  494. else if (num.equals("4")) // 返回星期四所在的日期
  495. c.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
  496. else if (num.equals("5")) // 返回星期五所在的日期
  497. c.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
  498. else if (num.equals("6")) // 返回星期六所在的日期
  499. c.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
  500. else if (num.equals("0")) // 返回星期日所在的日期
  501. c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
  502. return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
  503. }
  504. /**
  505. * 根据一个日期,返回是星期几的字符串
  506. *
  507. * @param sdate
  508. * @return
  509. */
  510. public static String getWeek(String sdate) {
  511. // 再转换为时间
  512. Date date = DateUtil.strToDate(sdate, YYYY_MM_DD);
  513. Calendar c = Calendar.getInstance();
  514. c.setTime(date);
  515. // int hour=c.get(Calendar.DAY_OF_WEEK);
  516. // hour中存的就是星期几了,其范围 1~7
  517. // 1=星期日 7=星期六,其他类推
  518. return new SimpleDateFormat("EEEE").format(c.getTime());
  519. }
  520. public static String getWeekStr(String sdate) {
  521. String str = "";
  522. str = DateUtil.getWeek(sdate);
  523. if ("1".equals(str)) {
  524. str = "星期日";
  525. } else if ("2".equals(str)) {
  526. str = "星期一";
  527. } else if ("3".equals(str)) {
  528. str = "星期二";
  529. } else if ("4".equals(str)) {
  530. str = "星期三";
  531. } else if ("5".equals(str)) {
  532. str = "星期四";
  533. } else if ("6".equals(str)) {
  534. str = "星期五";
  535. } else if ("7".equals(str)) {
  536. str = "星期六";
  537. }
  538. return str;
  539. }
  540. /**
  541. * 两个时间之间的天数
  542. *
  543. * @param date1
  544. * @param date2
  545. * @return
  546. */
  547. public static long getDays(String date1, String date2) {
  548. if (date1 == null || date1.equals(""))
  549. return 0;
  550. if (date2 == null || date2.equals(""))
  551. return 0;
  552. // 转换为标准时间
  553. SimpleDateFormat myFormatter = new SimpleDateFormat(YYYY_MM_DD);
  554. Date date = null;
  555. Date mydate = null;
  556. try {
  557. date = myFormatter.parse(date1);
  558. mydate = myFormatter.parse(date2);
  559. } catch (Exception e) {
  560. }
  561. long day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
  562. return day;
  563. }
  564. /**
  565. * 返回两个日期相差的天数
  566. * @param date1
  567. * @param date2
  568. * @return
  569. */
  570. public static long getDays(Date date1, Date date2) {
  571. if (date1 == null || date2 == null)
  572. return 0;
  573. long day = (date1.getTime() - date2.getTime()) / (24 * 60 * 60 * 1000);
  574. return day;
  575. }
  576. /**
  577. * 形成如下的日历 , 根据传入的一个时间返回一个结构 星期日 星期一 星期二 星期三 星期四 星期五 星期六 下面是当月的各个时间
  578. * 此函数返回该日历第一行星期日所在的日期
  579. *
  580. * @param sdate
  581. * @return
  582. */
  583. public static String getNowMonth(String sdate) {
  584. // 取该时间所在月的一号
  585. sdate = sdate.substring(0, 8) + "01";
  586. // 得到这个月的1号是星期几
  587. Date date = DateUtil.strToDate(sdate, YYYY_MM_DD);
  588. Calendar c = Calendar.getInstance();
  589. c.setTime(date);
  590. int u = c.get(Calendar.DAY_OF_WEEK);
  591. String newday = DateUtil.getNextDay(sdate, 1 - u);
  592. return newday;
  593. }
  594. /**
  595. * 取得数据库主键 生成格式为yyyymmddhhmmss+k位随机数
  596. *
  597. * @param k 表示是取几位随机数,可以自己定
  598. */
  599. public static String getNo(int k) {
  600. return getUserDate("yyyyMMddhhmmss") + getRandom(k);
  601. }
  602. /**
  603. * 返回一个随机数
  604. *
  605. * @param i
  606. * @return
  607. */
  608. public static String getRandom(int i) {
  609. Random jjj = new Random();
  610. if (i == 0)
  611. return "";
  612. String jj = "";
  613. for (int k = 0; k < i; k++) {
  614. jj = jj + jjj.nextInt(9);
  615. }
  616. return jj;
  617. }
  618. /**
  619. * 根据用户生日计算年龄
  620. */
  621. public static int getAgeByBirthday(Date birthday) {
  622. try {
  623. Calendar cal = Calendar.getInstance();
  624. if (cal.before(birthday)) {
  625. return 0;
  626. }
  627. int yearNow = cal.get(Calendar.YEAR);
  628. int monthNow = cal.get(Calendar.MONTH) + 1;
  629. int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
  630. cal.setTime(birthday);
  631. int yearBirth = cal.get(Calendar.YEAR);
  632. int monthBirth = cal.get(Calendar.MONTH) + 1;
  633. int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);
  634. int age = yearNow - yearBirth;
  635. if (monthNow <= monthBirth) {
  636. if (monthNow == monthBirth) {
  637. if (dayOfMonthNow < dayOfMonthBirth) {
  638. age--;
  639. }
  640. } else {
  641. age--;
  642. }
  643. }
  644. return age;
  645. } catch (Exception e) {
  646. return 0;
  647. }
  648. }
  649. public static void main(String[] args) {
  650. // String hour = "12:22:12";
  651. // System.out.println(Time.valueOf(hour));
  652. // System.out.println(getNowDate());
  653. //
  654. System.out.println(getNextYear(new Date(), -65));
  655. }
  656. public static int getNowMonth(){
  657. Calendar cal = Calendar.getInstance();
  658. return cal.get(Calendar.MONTH)+1;
  659. }
  660. public static int getNowYear(){
  661. Calendar cal = Calendar.getInstance();
  662. return cal.get(Calendar.YEAR);
  663. }
  664. }