DateUtil.java 18 KB

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