DateUtil.java 20 KB

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