DateUtil.java 22 KB

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