DateUtil.java 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  1. package com.yihu.jw.util;
  2. import org.apache.commons.lang3.StringUtils;
  3. import java.sql.Timestamp;
  4. import java.text.DateFormat;
  5. import java.text.ParsePosition;
  6. import java.text.SimpleDateFormat;
  7. import java.util.*;
  8. public class DateUtil {
  9. public static final String HH_MM = "HH:mm";
  10. public static final String HH_MM_SS = "HH:mm:ss";
  11. public static final String YY = "yy";
  12. public static final String YYYYMM = "yyyyMM";
  13. public static final String YYYYMMDD = "yyyyMMdd";
  14. public static final String YYYY_MM_DD = "yyyy-MM-dd";
  15. public static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
  16. public static final String YYYY_MM_DD_HH = "yyyy-MM-dd HH";
  17. public static final String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
  18. public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
  19. public static final String YYYY_M_D_HH_MM_SS = "yyyy/M/d HH:mm:ss";
  20. /**
  21. * 时间格式转中文格式
  22. */
  23. public static String dateToChinese(Date date) {
  24. SimpleDateFormat formatter = new SimpleDateFormat( "yyyy年MM月dd日 EEEEaaaa hh:mm", Locale.CHINA);
  25. return formatter.format(date);
  26. }
  27. /*public static void main(String[] args)
  28. {
  29. System.out.print(dateToChinese(new 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(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(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(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(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. Date date = myFormatter.parse(sj1);
  315. 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. * 时间前推或后推天数(负数前推正数后推)
  339. * date 基准时间
  340. */
  341. public static Date getPreDays(Date date, int days) {
  342. Date day = null;
  343. try {
  344. Calendar c = Calendar.getInstance();
  345. c.setTime(date);
  346. c.add(Calendar.DATE, days);
  347. day = c.getTime();
  348. } catch (Exception e) {
  349. }
  350. return day;
  351. }
  352. /**
  353. * 得到一个时间延后或前移几分钟的时间,nowdate为时间,delay为前移或后延的分钟数
  354. */
  355. public static Date getNextMin(Date date, int delay) {
  356. try {
  357. Calendar cal = Calendar.getInstance();
  358. cal.setTime(date);
  359. cal.add(Calendar.MINUTE, delay);
  360. return cal.getTime();
  361. } catch (Exception e) {
  362. return null;
  363. }
  364. }
  365. /**
  366. * 得到一个时间延后或前移几天的时间,nowdate为时间,delay为前移或后延的天数
  367. */
  368. public static String getNextDay(String nowdate, int delay) {
  369. try {
  370. SimpleDateFormat format = new SimpleDateFormat(YYYY_MM_DD);
  371. String mdate = "";
  372. Date d = strToDate(nowdate, YYYY_MM_DD);
  373. long myTime = (d.getTime() / 1000) + delay * 24 * 60 * 60;
  374. d.setTime(myTime * 1000);
  375. mdate = format.format(d);
  376. return mdate;
  377. } catch (Exception e) {
  378. return "";
  379. }
  380. }
  381. // public static String getNextDay(Date d, int delay) {
  382. // try {
  383. // SimpleDateFormat format = new SimpleDateFormat(YYYY_MM_DD);
  384. // String mdate = "";
  385. // long myTime = (d.getTime() / 1000) + delay * 24 * 60 * 60;
  386. // d.setTime(myTime * 1000);
  387. // mdate = format.format(d);
  388. // return mdate;
  389. // } catch (Exception e) {
  390. // return "";
  391. // }
  392. // }
  393. public static String getNextDay(Date d, int days) {
  394. Calendar c = Calendar.getInstance();
  395. c.setTime(d);
  396. c.add(Calendar.DATE, days);
  397. return dateToStrShort(c.getTime());
  398. }
  399. public static String getNextMonth(Date d, int months) {
  400. Calendar c = Calendar.getInstance();
  401. c.setTime(d);
  402. c.add(Calendar.MONTH, months);
  403. return dateToStrShort(c.getTime());
  404. }
  405. public static String getNextYear(Date d, int year) {
  406. Calendar c = Calendar.getInstance();
  407. c.setTime(d);
  408. c.add(Calendar.YEAR, year);
  409. return dateToStrShort(c.getTime());
  410. }
  411. /**
  412. * 获取本月第一天
  413. * @return
  414. */
  415. public static String getCurMonthFirstDayShort(){
  416. Calendar c = Calendar.getInstance();
  417. c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
  418. return dateToStrShort(c.getTime());
  419. }
  420. /**
  421. * 判断是否润年
  422. *
  423. * @param ddate
  424. * @return
  425. */
  426. public static boolean isLeapYear(String ddate) {
  427. /**
  428. * 详细设计: 1.被400整除是闰年,否则: 2.不能被4整除则不是闰年 3.能被4整除同时不能被100整除则是闰年
  429. * 3.能被4整除同时能被100整除则不是闰年
  430. */
  431. Date d = strToDate(ddate, YYYY_MM_DD);
  432. GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
  433. gc.setTime(d);
  434. int year = gc.get(Calendar.YEAR);
  435. if ((year % 400) == 0)
  436. return true;
  437. else if ((year % 4) == 0) {
  438. if ((year % 100) == 0)
  439. return false;
  440. else
  441. return true;
  442. } else
  443. return false;
  444. }
  445. /**
  446. * 返回美国时间格式 26 Apr 2006
  447. *
  448. * @param str
  449. * @return
  450. */
  451. public static String getEDate(String str) {
  452. SimpleDateFormat formatter = new SimpleDateFormat(YYYY_MM_DD);
  453. ParsePosition pos = new ParsePosition(0);
  454. Date strtodate = formatter.parse(str, pos);
  455. String j = strtodate.toString();
  456. String[] k = j.split(" ");
  457. return k[2] + k[1].toUpperCase() + k[5].substring(2, 4);
  458. }
  459. /**
  460. * 获取一个月的最后一天
  461. *
  462. * @param dat
  463. * @return
  464. */
  465. public static String getEndDateOfMonth(String dat) {// yyyy-MM-dd
  466. String str = dat.substring(0, 8);
  467. String month = dat.substring(5, 7);
  468. int mon = Integer.parseInt(month);
  469. if (mon == 1 || mon == 3 || mon == 5 || mon == 7 || mon == 8 || mon == 10 || mon == 12) {
  470. str += "31";
  471. } else if (mon == 4 || mon == 6 || mon == 9 || mon == 11) {
  472. str += "30";
  473. } else {
  474. if (isLeapYear(dat)) {
  475. str += "29";
  476. } else {
  477. str += "28";
  478. }
  479. }
  480. return str;
  481. }
  482. /**
  483. * 判断二个时间是否在同一个周
  484. *
  485. * @param date1
  486. * @param date2
  487. * @return
  488. */
  489. public static boolean isSameWeekDates(Date date1, Date date2) {
  490. Calendar cal1 = Calendar.getInstance();
  491. Calendar cal2 = Calendar.getInstance();
  492. cal1.setTime(date1);
  493. cal2.setTime(date2);
  494. int subYear = cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR);
  495. if (0 == subYear) {
  496. if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
  497. return true;
  498. } else if (1 == subYear && 11 == cal2.get(Calendar.MONTH)) {
  499. // 如果12月的最后一周横跨来年第一周的话则最后一周即算做来年的第一周
  500. if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
  501. return true;
  502. } else if (-1 == subYear && 11 == cal1.get(Calendar.MONTH)) {
  503. if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
  504. return true;
  505. }
  506. return false;
  507. }
  508. /**
  509. * 产生周序列,即得到当前时间所在的年度是第几周
  510. *
  511. * @return
  512. */
  513. public static String getSeqWeek() {
  514. Calendar c = Calendar.getInstance(Locale.CHINA);
  515. String week = Integer.toString(c.get(Calendar.WEEK_OF_YEAR));
  516. if (week.length() == 1)
  517. week = "0" + week;
  518. String year = Integer.toString(c.get(Calendar.YEAR));
  519. return year + week;
  520. }
  521. /**
  522. * 获得一个日期所在的周的星期几的日期,如要找出2002年2月3日所在周的星期一是几号
  523. *
  524. * @param sdate
  525. * @param num
  526. * @return
  527. */
  528. public static String getWeek(String sdate, String num) {
  529. // 再转换为时间
  530. Date dd = DateUtil.strToDate(sdate, YYYY_MM_DD);
  531. Calendar c = Calendar.getInstance();
  532. c.setTime(dd);
  533. if (num.equals("1")) // 返回星期一所在的日期
  534. c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
  535. else if (num.equals("2")) // 返回星期二所在的日期
  536. c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
  537. else if (num.equals("3")) // 返回星期三所在的日期
  538. c.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);
  539. else if (num.equals("4")) // 返回星期四所在的日期
  540. c.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
  541. else if (num.equals("5")) // 返回星期五所在的日期
  542. c.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
  543. else if (num.equals("6")) // 返回星期六所在的日期
  544. c.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
  545. else if (num.equals("0")) // 返回星期日所在的日期
  546. c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
  547. return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
  548. }
  549. /**
  550. * 获取2个时间点的工作日天数
  551. * @param startDate 开始时间
  552. * @param endDate 结束时间
  553. * @return
  554. */
  555. public static int getDutyDays(Date startDate, Date endDate) {
  556. int result = 0;
  557. Date startTime = new Date(startDate.getTime());
  558. while (startTime.compareTo(endDate) <= 0) {
  559. if (startTime.getDay() != 6 && startTime.getDay() != 0) {
  560. result++;
  561. }
  562. startTime.setDate(startTime.getDate() + 1);
  563. }
  564. return result;
  565. }
  566. /**
  567. * 根据一个日期,返回是星期几的字符串
  568. *
  569. * @param sdate
  570. * @return
  571. */
  572. public static String getWeek(String sdate) {
  573. // 再转换为时间
  574. Date date = DateUtil.strToDate(sdate, YYYY_MM_DD);
  575. Calendar c = Calendar.getInstance();
  576. c.setTime(date);
  577. // int hour=c.get(Calendar.DAY_OF_WEEK);
  578. // hour中存的就是星期几了,其范围 1~7
  579. // 1=星期日 7=星期六,其他类推
  580. return new SimpleDateFormat("EEEE").format(c.getTime());
  581. }
  582. public static String getWeekStr(String sdate) {
  583. String str = "";
  584. str = DateUtil.getWeek(sdate);
  585. if ("1".equals(str)) {
  586. str = "星期日";
  587. } else if ("2".equals(str)) {
  588. str = "星期一";
  589. } else if ("3".equals(str)) {
  590. str = "星期二";
  591. } else if ("4".equals(str)) {
  592. str = "星期三";
  593. } else if ("5".equals(str)) {
  594. str = "星期四";
  595. } else if ("6".equals(str)) {
  596. str = "星期五";
  597. } else if ("7".equals(str)) {
  598. str = "星期六";
  599. }
  600. return str;
  601. }
  602. /**
  603. * 两个时间之间的天数
  604. *
  605. * @param date1
  606. * @param date2
  607. * @return
  608. */
  609. public static long getDays(String date1, String date2) {
  610. if (date1 == null || date1.equals(""))
  611. return 0;
  612. if (date2 == null || date2.equals(""))
  613. return 0;
  614. // 转换为标准时间
  615. SimpleDateFormat myFormatter = new SimpleDateFormat(YYYY_MM_DD);
  616. Date date = null;
  617. Date mydate = null;
  618. try {
  619. date = myFormatter.parse(date1);
  620. mydate = myFormatter.parse(date2);
  621. } catch (Exception e) {
  622. }
  623. long day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
  624. return day;
  625. }
  626. /**
  627. * 返回两个日期相差的天数
  628. * @param date1
  629. * @param date2
  630. * @return
  631. */
  632. public static long getDays(Date date1, Date date2) {
  633. if (date1 == null || date2 == null)
  634. return 0;
  635. long day = (date1.getTime() - date2.getTime()) / (24 * 60 * 60 * 1000);
  636. return day;
  637. }
  638. /**
  639. * 形成如下的日历 , 根据传入的一个时间返回一个结构 星期日 星期一 星期二 星期三 星期四 星期五 星期六 下面是当月的各个时间
  640. * 此函数返回该日历第一行星期日所在的日期
  641. *
  642. * @param sdate
  643. * @return
  644. */
  645. public static String getNowMonth(String sdate) {
  646. // 取该时间所在月的一号
  647. sdate = sdate.substring(0, 8) + "01";
  648. // 得到这个月的1号是星期几
  649. Date date = DateUtil.strToDate(sdate, YYYY_MM_DD);
  650. Calendar c = Calendar.getInstance();
  651. c.setTime(date);
  652. int u = c.get(Calendar.DAY_OF_WEEK);
  653. String newday = DateUtil.getNextDay(sdate, 1 - u);
  654. return newday;
  655. }
  656. /**
  657. * 取得数据库主键 生成格式为yyyymmddhhmmss+k位随机数
  658. *
  659. * @param k 表示是取几位随机数,可以自己定
  660. */
  661. public static String getNo(int k) {
  662. return getUserDate("yyyyMMddhhmmss") + getRandom(k);
  663. }
  664. /**
  665. * 返回一个随机数
  666. *
  667. * @param i
  668. * @return
  669. */
  670. public static String getRandom(int i) {
  671. Random jjj = new Random();
  672. if (i == 0)
  673. return "";
  674. String jj = "";
  675. for (int k = 0; k < i; k++) {
  676. jj = jj + jjj.nextInt(9);
  677. }
  678. return jj;
  679. }
  680. /**
  681. * 根据用户生日计算年龄
  682. */
  683. public static int getAgeByBirthday(Date birthday) {
  684. try {
  685. int age = 0;
  686. if (birthday == null) {
  687. return age;
  688. }
  689. String birth = new SimpleDateFormat("yyyyMMdd").format(birthday);
  690. int year = Integer.valueOf(birth.substring(0, 4));
  691. int month = Integer.valueOf(birth.substring(4, 6));
  692. int day = Integer.valueOf(birth.substring(6));
  693. Calendar cal = Calendar.getInstance();
  694. age = cal.get(Calendar.YEAR) - year;
  695. //周岁计算
  696. if (cal.get(Calendar.MONTH) < (month - 1) || (cal.get(Calendar.MONTH) == (month - 1) && cal.get(Calendar.DATE) < day)) {
  697. age--;
  698. }
  699. return age;
  700. } catch (Exception e) {
  701. return 0;
  702. }
  703. }
  704. /**
  705. * 字符串转时间
  706. * @param str 时间字符串
  707. * @param eg 格式
  708. * @return
  709. */
  710. public static Date stringToDate(String str, String eg) {
  711. DateFormat format = new SimpleDateFormat(eg);
  712. Date date = null;
  713. if (str != null && !"".equals(str)) {
  714. try {
  715. date = format.parse(str);
  716. } catch (Exception e) {
  717. return null;
  718. }
  719. }
  720. return date;
  721. }
  722. public static int getNowMonth(){
  723. Calendar cal = Calendar.getInstance();
  724. return cal.get(Calendar.MONTH)+1;
  725. }
  726. public static int getNowYear(){
  727. Calendar cal = Calendar.getInstance();
  728. return cal.get(Calendar.YEAR);
  729. }
  730. /**
  731. * 获取周一
  732. * @return
  733. */
  734. public static String getMondayOfThisWeek() {
  735. SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd");
  736. Calendar c = Calendar.getInstance();
  737. int day_of_week = c.get(Calendar.DAY_OF_WEEK) - 1;
  738. if (day_of_week == 0)
  739. day_of_week = 7;
  740. c.add(Calendar.DATE, -day_of_week + 1);
  741. return df2.format(c.getTime());
  742. }
  743. /**
  744. * 得到本周周日
  745. *
  746. * @return yyyy-MM-dd
  747. */
  748. public static String getSundayOfThisWeek() {
  749. SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd");
  750. Calendar c = Calendar.getInstance();
  751. int day_of_week = c.get(Calendar.DAY_OF_WEEK) - 1;
  752. if (day_of_week == 0)
  753. day_of_week = 7;
  754. c.add(Calendar.DATE, -day_of_week + 7);
  755. return df2.format(c.getTime());
  756. }
  757. /**
  758. * 获取当月第一天
  759. * @return
  760. */
  761. public static String getFristDayOfMonth() {
  762. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  763. // 获取前月的第一天
  764. Calendar c = Calendar.getInstance();
  765. c.add(Calendar.MONTH, 0);
  766. c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
  767. String first = format.format(c.getTime());
  768. return format.format(c.getTime());
  769. }
  770. /**
  771. * 获取当月最后一天
  772. */
  773. public static String getLastDayOfMonth(){
  774. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  775. // 获取前月的第一天
  776. Calendar ca = Calendar.getInstance();
  777. ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
  778. return format.format(ca.getTime());
  779. }
  780. public static int getSignYear(){
  781. Calendar ca = Calendar.getInstance();
  782. if(getNowMonth()>=4){
  783. return getNowYear();
  784. }
  785. return getNowYear()-1;
  786. }
  787. /**
  788. * 计算预产期
  789. * 末次月经开始日期(第一天),月份+9,日期+7
  790. * @param date
  791. * @return
  792. */
  793. public static Date getDueDate(Date date){
  794. Calendar cal = Calendar.getInstance();
  795. cal.setTime(date);
  796. cal.add(Calendar.MONTH,9);
  797. cal.add(Calendar.DAY_OF_YEAR,7);
  798. return cal.getTime();
  799. }
  800. /**
  801. * 计算产检时间
  802. * @param date
  803. * @param day
  804. * @return
  805. */
  806. public static Date getPrenatalInspectorDate(Date date,Integer day){
  807. Calendar cal = Calendar.getInstance();
  808. cal.setTime(date);
  809. cal.add(Calendar.DAY_OF_YEAR,day);
  810. return cal.getTime();
  811. }
  812. /**
  813. * 转换日期字符串为Timestamp YYYY-MM-DD HH:MM:SS
  814. * @return
  815. */
  816. public static Timestamp fomrmatStringToTimeStamp(String dateStr){
  817. Timestamp ts = Timestamp.valueOf(dateStr);
  818. return ts;
  819. }
  820. /**
  821. * 获取当前时间的Timestamp
  822. * @return
  823. */
  824. public static Timestamp getNowTimestamp(){
  825. Date date = new Date();
  826. Timestamp nousedate = new Timestamp(date.getTime());
  827. return nousedate;
  828. }
  829. /**
  830. * 获取周一
  831. * @return
  832. */
  833. public static String getSundayOfThisDate(Date date) {
  834. SimpleDateFormat df2 = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
  835. return df2.format(getSundayOfThisDateToDate(date));
  836. }
  837. /**
  838. * 得到本周周日
  839. *
  840. * @return yyyy-MM-dd
  841. */
  842. public static Date getSundayOfThisDateToDate(Date date) {
  843. Calendar c = Calendar.getInstance();
  844. c.setTime(date);
  845. int day_of_week = c.get(Calendar.DAY_OF_WEEK) - 1;
  846. if (day_of_week == 0)
  847. day_of_week = 7;
  848. c.add(Calendar.DATE, -day_of_week + 7);
  849. return c.getTime();
  850. }
  851. }