DateUtil.java 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104
  1. package com.yihu.wlyy.util;
  2. import org.apache.commons.collections.map.HashedMap;
  3. import org.apache.commons.lang3.StringUtils;
  4. import java.sql.Time;
  5. import java.sql.Timestamp;
  6. import java.text.DateFormat;
  7. import java.text.ParsePosition;
  8. import java.text.SimpleDateFormat;
  9. import java.util.*;
  10. public class DateUtil {
  11. public static final String HH_MM = "HH:mm";
  12. public static final String HH_MM_SS = "HH:mm:ss";
  13. public static final String YY = "yy";
  14. public static final String YYYYMM = "yyyyMM";
  15. public static final String YYYYMMDD = "yyyyMMdd";
  16. public static final String YYYY_MM_DD = "yyyy-MM-dd";
  17. public static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
  18. public static final String YYYY_MM_DD_HH = "yyyy-MM-dd HH";
  19. public static final String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
  20. public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
  21. public static final String YYYY_M_D_HH_MM_SS = "yyyy/M/d HH:mm:ss";
  22. public static final String YYYYMMddHHmmssSSS = "yyyyMMddHHmmssSSS";
  23. public static final String YYYY_MM ="yyyy-MM";
  24. /**
  25. * 时间格式转中文格式
  26. */
  27. public static String dateToChinese(Date date) {
  28. SimpleDateFormat formatter = new SimpleDateFormat( "yyyy年MM月dd日 EEEEaaaa hh:mm", Locale.CHINA);
  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 Date getNextMonthReturnDate(Date d, int months) {
  391. Calendar c = Calendar.getInstance();
  392. c.setTime(d);
  393. c.add(Calendar.MONTH, months);
  394. return c.getTime();
  395. }
  396. public static String getNextYear(Date d, int year) {
  397. Calendar c = Calendar.getInstance();
  398. c.setTime(d);
  399. c.add(Calendar.YEAR, year);
  400. return dateToStrShort(c.getTime());
  401. }
  402. /**
  403. * 获取本月第一天
  404. * @return
  405. */
  406. public static String getCurMonthFirstDayShort(){
  407. Calendar c = Calendar.getInstance();
  408. c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
  409. return dateToStrShort(c.getTime());
  410. }
  411. /**
  412. * 判断是否润年
  413. *
  414. * @param ddate
  415. * @return
  416. */
  417. public static boolean isLeapYear(String ddate) {
  418. /**
  419. * 详细设计: 1.被400整除是闰年,否则: 2.不能被4整除则不是闰年 3.能被4整除同时不能被100整除则是闰年
  420. * 3.能被4整除同时能被100整除则不是闰年
  421. */
  422. Date d = strToDate(ddate, YYYY_MM_DD);
  423. GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
  424. gc.setTime(d);
  425. int year = gc.get(Calendar.YEAR);
  426. if ((year % 400) == 0)
  427. return true;
  428. else if ((year % 4) == 0) {
  429. if ((year % 100) == 0)
  430. return false;
  431. else
  432. return true;
  433. } else
  434. return false;
  435. }
  436. /**
  437. * 返回美国时间格式 26 Apr 2006
  438. *
  439. * @param str
  440. * @return
  441. */
  442. public static String getEDate(String str) {
  443. SimpleDateFormat formatter = new SimpleDateFormat(YYYY_MM_DD);
  444. ParsePosition pos = new ParsePosition(0);
  445. Date strtodate = formatter.parse(str, pos);
  446. String j = strtodate.toString();
  447. String[] k = j.split(" ");
  448. return k[2] + k[1].toUpperCase() + k[5].substring(2, 4);
  449. }
  450. /**
  451. * 获取一个月的最后一天
  452. *
  453. * @param dat
  454. * @return
  455. */
  456. public static String getEndDateOfMonth(String dat) {// yyyy-MM-dd
  457. String str = dat.substring(0, 8);
  458. String month = dat.substring(5, 7);
  459. int mon = Integer.parseInt(month);
  460. if (mon == 1 || mon == 3 || mon == 5 || mon == 7 || mon == 8 || mon == 10 || mon == 12) {
  461. str += "31";
  462. } else if (mon == 4 || mon == 6 || mon == 9 || mon == 11) {
  463. str += "30";
  464. } else {
  465. if (isLeapYear(dat)) {
  466. str += "29";
  467. } else {
  468. str += "28";
  469. }
  470. }
  471. return str;
  472. }
  473. /**
  474. * 判断二个时间是否在同一个周
  475. *
  476. * @param date1
  477. * @param date2
  478. * @return
  479. */
  480. public static boolean isSameWeekDates(Date date1, Date date2) {
  481. Calendar cal1 = Calendar.getInstance();
  482. Calendar cal2 = Calendar.getInstance();
  483. cal1.setTime(date1);
  484. cal2.setTime(date2);
  485. int subYear = cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR);
  486. if (0 == subYear) {
  487. if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
  488. return true;
  489. } else if (1 == subYear && 11 == cal2.get(Calendar.MONTH)) {
  490. // 如果12月的最后一周横跨来年第一周的话则最后一周即算做来年的第一周
  491. if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
  492. return true;
  493. } else if (-1 == subYear && 11 == cal1.get(Calendar.MONTH)) {
  494. if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
  495. return true;
  496. }
  497. return false;
  498. }
  499. /**
  500. * 产生周序列,即得到当前时间所在的年度是第几周
  501. *
  502. * @return
  503. */
  504. public static String getSeqWeek() {
  505. Calendar c = Calendar.getInstance(Locale.CHINA);
  506. String week = Integer.toString(c.get(Calendar.WEEK_OF_YEAR));
  507. if (week.length() == 1)
  508. week = "0" + week;
  509. String year = Integer.toString(c.get(Calendar.YEAR));
  510. return year + week;
  511. }
  512. /**
  513. * 获得一个日期所在的周的星期几的日期,如要找出2002年2月3日所在周的星期一是几号
  514. *
  515. * @param sdate
  516. * @param num
  517. * @return
  518. */
  519. public static String getWeek(String sdate, String num) {
  520. // 再转换为时间
  521. Date dd = DateUtil.strToDate(sdate, YYYY_MM_DD);
  522. Calendar c = Calendar.getInstance();
  523. c.setTime(dd);
  524. if (num.equals("1")) // 返回星期一所在的日期
  525. c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
  526. else if (num.equals("2")) // 返回星期二所在的日期
  527. c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
  528. else if (num.equals("3")) // 返回星期三所在的日期
  529. c.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);
  530. else if (num.equals("4")) // 返回星期四所在的日期
  531. c.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
  532. else if (num.equals("5")) // 返回星期五所在的日期
  533. c.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
  534. else if (num.equals("6")) // 返回星期六所在的日期
  535. c.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
  536. else if (num.equals("0")) // 返回星期日所在的日期
  537. c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
  538. return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
  539. }
  540. /**
  541. * 根据一个日期,返回是星期几的字符串
  542. *
  543. * @param sdate
  544. * @return
  545. */
  546. public static String getWeek(String sdate) {
  547. // 再转换为时间
  548. Date date = DateUtil.strToDate(sdate, YYYY_MM_DD);
  549. Calendar c = Calendar.getInstance();
  550. c.setTime(date);
  551. // int hour=c.get(Calendar.DAY_OF_WEEK);
  552. // hour中存的就是星期几了,其范围 1~7
  553. // 1=星期日 7=星期六,其他类推
  554. return new SimpleDateFormat("EEEE").format(c.getTime());
  555. }
  556. public static String getWeekStr(String sdate) {
  557. String str = "";
  558. str = DateUtil.getWeek(sdate);
  559. if ("1".equals(str)) {
  560. str = "星期日";
  561. } else if ("2".equals(str)) {
  562. str = "星期一";
  563. } else if ("3".equals(str)) {
  564. str = "星期二";
  565. } else if ("4".equals(str)) {
  566. str = "星期三";
  567. } else if ("5".equals(str)) {
  568. str = "星期四";
  569. } else if ("6".equals(str)) {
  570. str = "星期五";
  571. } else if ("7".equals(str)) {
  572. str = "星期六";
  573. }
  574. return str;
  575. }
  576. /**
  577. * 两个时间之间的天数
  578. *
  579. * @param date1
  580. * @param date2
  581. * @return
  582. */
  583. public static long getDays(String date1, String date2) {
  584. if (date1 == null || date1.equals(""))
  585. return 0;
  586. if (date2 == null || date2.equals(""))
  587. return 0;
  588. // 转换为标准时间
  589. SimpleDateFormat myFormatter = new SimpleDateFormat(YYYY_MM_DD);
  590. java.util.Date date = null;
  591. java.util.Date mydate = null;
  592. try {
  593. date = myFormatter.parse(date1);
  594. mydate = myFormatter.parse(date2);
  595. } catch (Exception e) {
  596. }
  597. long day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
  598. return day;
  599. }
  600. /**
  601. * 返回两个日期相差的天数
  602. * @param date1
  603. * @param date2
  604. * @return
  605. */
  606. public static long getDays(Date date1, Date date2) {
  607. if (date1 == null || date2 == null)
  608. return 0;
  609. long day = (date1.getTime() - date2.getTime()) / (24 * 60 * 60 * 1000);
  610. return day;
  611. }
  612. /**
  613. * 形成如下的日历 , 根据传入的一个时间返回一个结构 星期日 星期一 星期二 星期三 星期四 星期五 星期六 下面是当月的各个时间
  614. * 此函数返回该日历第一行星期日所在的日期
  615. *
  616. * @param sdate
  617. * @return
  618. */
  619. public static String getNowMonth(String sdate) {
  620. // 取该时间所在月的一号
  621. sdate = sdate.substring(0, 8) + "01";
  622. // 得到这个月的1号是星期几
  623. Date date = DateUtil.strToDate(sdate, YYYY_MM_DD);
  624. Calendar c = Calendar.getInstance();
  625. c.setTime(date);
  626. int u = c.get(Calendar.DAY_OF_WEEK);
  627. String newday = DateUtil.getNextDay(sdate, 1 - u);
  628. return newday;
  629. }
  630. /**
  631. * 取得数据库主键 生成格式为yyyymmddhhmmss+k位随机数
  632. *
  633. * @param k 表示是取几位随机数,可以自己定
  634. */
  635. public static String getNo(int k) {
  636. return getUserDate("yyyyMMddhhmmss") + getRandom(k);
  637. }
  638. /**
  639. * 取得流水号 生成格式为yyyymmddhhmmss+k位随机UUID
  640. *
  641. * @param k 表示是取几位随机数,可以自己定
  642. */
  643. public static String getUidNo(int k) {
  644. return getUserDate("yyyyMMddhhmmss") + UUID.randomUUID().toString().replaceAll("-", "").substring(0, k);
  645. }
  646. /**
  647. * 返回一个随机数
  648. *
  649. * @param i
  650. * @return
  651. */
  652. public static String getRandom(int i) {
  653. Random jjj = new Random();
  654. if (i == 0)
  655. return "";
  656. String jj = "";
  657. for (int k = 0; k < i; k++) {
  658. jj = jj + jjj.nextInt(9);
  659. }
  660. return jj;
  661. }
  662. /**
  663. * 根据用户生日计算年龄
  664. */
  665. public static int getAgeByBirthday(Date birthday) {
  666. try {
  667. int age = 0;
  668. if (birthday == null) {
  669. return age;
  670. }
  671. String birth = new SimpleDateFormat("yyyyMMdd").format(birthday);
  672. int year = Integer.valueOf(birth.substring(0, 4));
  673. int month = Integer.valueOf(birth.substring(4, 6));
  674. int day = Integer.valueOf(birth.substring(6));
  675. Calendar cal = Calendar.getInstance();
  676. age = cal.get(Calendar.YEAR) - year;
  677. //周岁计算
  678. if (cal.get(Calendar.MONTH) < (month - 1) || (cal.get(Calendar.MONTH) == (month - 1) && cal.get(Calendar.DATE) < day)) {
  679. age--;
  680. }
  681. return age;
  682. } catch (Exception e) {
  683. return 0;
  684. }
  685. }
  686. /**
  687. * 字符串转时间
  688. * @param str 时间字符串
  689. * @param eg 格式
  690. * @return
  691. */
  692. public static Date stringToDate(String str, String eg) {
  693. DateFormat format = new SimpleDateFormat(eg);
  694. Date date = null;
  695. if (str != null && !"".equals(str)) {
  696. try {
  697. date = format.parse(str);
  698. } catch (Exception e) {
  699. return null;
  700. }
  701. }
  702. return date;
  703. }
  704. public static int getNowMonth(){
  705. Calendar cal = Calendar.getInstance();
  706. return cal.get(Calendar.MONTH)+1;
  707. }
  708. public static int getNowYear(){
  709. Calendar cal = Calendar.getInstance();
  710. return cal.get(Calendar.YEAR);
  711. }
  712. /**
  713. * 获取周一
  714. * @return
  715. */
  716. public static String getMondayOfThisWeek() {
  717. SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd");
  718. Calendar c = Calendar.getInstance();
  719. int day_of_week = c.get(Calendar.DAY_OF_WEEK) - 1;
  720. if (day_of_week == 0)
  721. day_of_week = 7;
  722. c.add(Calendar.DATE, -day_of_week + 1);
  723. return df2.format(c.getTime());
  724. }
  725. /**
  726. * 得到本周周日
  727. *
  728. * @return yyyy-MM-dd
  729. */
  730. public static String getSundayOfThisWeek() {
  731. SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd");
  732. Calendar c = Calendar.getInstance();
  733. int day_of_week = c.get(Calendar.DAY_OF_WEEK) - 1;
  734. if (day_of_week == 0)
  735. day_of_week = 7;
  736. c.add(Calendar.DATE, -day_of_week + 7);
  737. return df2.format(c.getTime());
  738. }
  739. /**
  740. * 获取当月第一天
  741. * @return
  742. */
  743. public static String getFristDayOfMonth() {
  744. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  745. // 获取前月的第一天
  746. Calendar c = Calendar.getInstance();
  747. c.add(Calendar.MONTH, 0);
  748. c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
  749. String first = format.format(c.getTime());
  750. return format.format(c.getTime());
  751. }
  752. /**
  753. * 获取当月最后一天
  754. */
  755. public static String getLastDayOfMonth(){
  756. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  757. // 获取前月的第一天
  758. Calendar ca = Calendar.getInstance();
  759. ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
  760. return format.format(ca.getTime());
  761. }
  762. public static int getSignYear(){
  763. Calendar ca = Calendar.getInstance();
  764. if(getNowMonth()>=4){
  765. return getNowYear();
  766. }
  767. return getNowYear()-1;
  768. }
  769. /**
  770. * 计算预产期
  771. * 末次月经开始日期(第一天),月份+9,日期+7
  772. * @param date
  773. * @return
  774. */
  775. public static Date getDueDate(Date date){
  776. Calendar cal = Calendar.getInstance();
  777. cal.setTime(date);
  778. cal.add(Calendar.MONTH,9);
  779. cal.add(Calendar.DAY_OF_YEAR,7);
  780. return cal.getTime();
  781. }
  782. /**
  783. * 计算产检时间
  784. * @param date
  785. * @param day
  786. * @return
  787. */
  788. public static Date getPrenatalInspectorDate(Date date,Integer day){
  789. Calendar cal = Calendar.getInstance();
  790. cal.setTime(date);
  791. cal.add(Calendar.DAY_OF_YEAR,day);
  792. return cal.getTime();
  793. }
  794. /**
  795. * 将HH:MM格式的字符串转TIME
  796. * @param hhmm
  797. * @return
  798. */
  799. public static Time hhmmStrToTime(String hhmm){
  800. Time time = null;
  801. DateFormat sdf = new SimpleDateFormat("hh:mm");
  802. try {
  803. Date date = sdf.parse(hhmm);
  804. time = new Time(date.getTime());
  805. } catch (Exception e) {
  806. e.printStackTrace();
  807. }
  808. return time;
  809. }
  810. /**
  811. * 获取当前时间的Timestamp
  812. * @return
  813. */
  814. public static Timestamp getNowTimestamp(){
  815. Date date = new Date();
  816. Timestamp nousedate = new Timestamp(date.getTime());
  817. return nousedate;
  818. }
  819. /**
  820. * 日期加减天数
  821. * @param date 时间
  822. * @param days 天数�?
  823. * @return
  824. */
  825. public static java.util.Date setDateTime(java.util.Date date,int days){
  826. Calendar cal = Calendar.getInstance();
  827. cal.setTime(date);
  828. cal.set(Calendar.DATE, cal.get(Calendar.DATE) +(days));
  829. return cal.getTime();
  830. }
  831. public static List<Map<String,Object>> findDates(Date dBegin, Date dEnd) {
  832. List<Map<String,Object>> lDate = new ArrayList();
  833. Map<String,Object> st = new HashedMap();
  834. st.put("date",DateUtil.dateToStr(dBegin,"yyyy-MM-dd"));
  835. st.put("avg",0);
  836. lDate.add(st);
  837. Calendar calBegin = Calendar.getInstance();
  838. // 使用给定的 Date 设置此 Calendar 的时间
  839. calBegin.setTime(dBegin);
  840. Calendar calEnd = Calendar.getInstance();
  841. // 使用给定的 Date 设置此 Calendar 的时间
  842. calEnd.setTime(dEnd);
  843. // 测试此日期是否在指定日期之后
  844. while (dEnd.after(calBegin.getTime())) {
  845. // 根据日历的规则,为给定的日历字段添加或减去指定的时间量
  846. calBegin.add(Calendar.DAY_OF_MONTH, 1);
  847. if(!dEnd.after(calBegin.getTime())){
  848. break;
  849. }
  850. Map<String,Object> stt = new HashedMap();
  851. stt.put("date",DateUtil.dateToStr(calBegin.getTime(),"yyyy-MM-dd"));
  852. stt.put("avg",0);
  853. lDate.add(stt);
  854. }
  855. return lDate;
  856. }
  857. public static List<Map<String,Object>> findDateASWeeks(Date dBegin, Date dEnd) {
  858. List<Map<String,Object>> lDate = new ArrayList();
  859. Calendar calBegin = Calendar.getInstance();
  860. // 使用给定的 Date 设置此 Calendar 的时间
  861. calBegin.setTime(dBegin);
  862. if(checkDateMonday(calBegin)){
  863. Map<String,Object> st = new HashedMap();
  864. st.put("date",DateUtil.dateToStr(dBegin,"yyyy-MM-dd"));
  865. st.put("avg",0);
  866. lDate.add(st);
  867. }
  868. Calendar calEnd = Calendar.getInstance();
  869. // 使用给定的 Date 设置此 Calendar 的时间
  870. calEnd.setTime(dEnd);
  871. // 测试此日期是否在指定日期之后
  872. while (dEnd.after(calBegin.getTime())) {
  873. // 根据日历的规则,为给定的日历字段添加或减去指定的时间量
  874. calBegin.add(Calendar.DAY_OF_MONTH, 1);
  875. if(!dEnd.after(calBegin.getTime())){
  876. break;
  877. }
  878. if(checkDateMonday(calBegin)){
  879. Map<String,Object> stt = new HashedMap();
  880. stt.put("date",DateUtil.dateToStr(calBegin.getTime(),"yyyy-MM-dd"));
  881. stt.put("avg",0);
  882. lDate.add(stt);
  883. }
  884. }
  885. return lDate;
  886. }
  887. public static List<Map<String,Object>> findDateASMonth(Date dBegin, Date dEnd) {
  888. List<Map<String,Object>> lDate = new ArrayList();
  889. Calendar calBegin = Calendar.getInstance();
  890. // 使用给定的 Date 设置此 Calendar 的时间
  891. calBegin.setTime(dBegin);
  892. if(checkFristDayOfDateMonth(calBegin)){
  893. Map<String,Object> st = new HashedMap();
  894. st.put("date",DateUtil.dateToStr(dBegin,"yyyy-MM"));
  895. st.put("avg",0);
  896. lDate.add(st);
  897. }
  898. Calendar calEnd = Calendar.getInstance();
  899. // 使用给定的 Date 设置此 Calendar 的时间
  900. calEnd.setTime(dEnd);
  901. // 测试此日期是否在指定日期之后
  902. while (dEnd.after(calBegin.getTime())) {
  903. // 根据日历的规则,为给定的日历字段添加或减去指定的时间量
  904. calBegin.add(Calendar.DAY_OF_MONTH, 1);
  905. if(!dEnd.after(calBegin.getTime())){
  906. break;
  907. }
  908. if(checkFristDayOfDateMonth(calBegin)){
  909. Map<String,Object> stt = new HashedMap();
  910. stt.put("date",DateUtil.dateToStr(calBegin.getTime(),"yyyy-MM"));
  911. stt.put("avg",0);
  912. lDate.add(stt);
  913. }
  914. }
  915. return lDate;
  916. }
  917. public static boolean checkDateMonday(Calendar cal){
  918. int week=cal.get(Calendar.DAY_OF_WEEK)-1;
  919. if(week ==1){
  920. return true;
  921. }else{
  922. return false;
  923. }
  924. }
  925. public static boolean checkFristDayOfDateMonth(Calendar cal){
  926. int today = cal.get(cal.DAY_OF_MONTH);
  927. if(today ==1){
  928. return true;
  929. }else{
  930. return false;
  931. }
  932. }
  933. public static List<Map<String,Object>> getLast6Month(){
  934. List<Map<String,Object>> rs = new ArrayList<>();
  935. Calendar dd = Calendar.getInstance();//定义日期实例
  936. Date endDate = new Date();
  937. dd.setTime(endDate);
  938. for(int i=1;i<7;i++){
  939. Map<String,Object> mc = new HashedMap();
  940. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
  941. String date = sdf.format(dd.getTime());
  942. mc.put("month",date);
  943. mc.put("count",0);
  944. dd.add(Calendar.MONTH,-1);
  945. rs.add(mc);
  946. }
  947. return rs;
  948. }
  949. /**
  950. * 获取去年日期
  951. * @return
  952. */
  953. public static String getLastYear(){
  954. Calendar cal = Calendar.getInstance();
  955. cal.add(Calendar.YEAR,-1);
  956. return dateToStrLong(cal.getTime());
  957. }
  958. /**
  959. * 获取儿童的年龄
  960. * @param date
  961. * @return
  962. */
  963. public static String getChildAge(Date date){
  964. Calendar now = Calendar.getInstance();
  965. Calendar cal = Calendar.getInstance();
  966. cal.setTime(date);
  967. int year = now.get(Calendar.YEAR)-cal.get(Calendar.YEAR);
  968. int month = now.get(Calendar.MONTH)-cal.get(Calendar.MONTH);
  969. if(month<0){
  970. year--;
  971. month+=12;
  972. }
  973. int day = now.get(Calendar.DAY_OF_MONTH)-cal.get(Calendar.DAY_OF_MONTH);
  974. if(day<0){
  975. month--;
  976. Calendar temp = Calendar.getInstance();
  977. temp.setTime(date);
  978. temp.add(Calendar.MONTH,1);
  979. temp.set(Calendar.DAY_OF_MONTH,1);
  980. temp.add(Calendar.DAY_OF_MONTH,-1);
  981. day = temp.get(Calendar.DAY_OF_MONTH)+now.get(Calendar.DAY_OF_MONTH)-cal.get(Calendar.DAY_OF_MONTH);
  982. }
  983. String y = year>0? year+"岁":"";
  984. String m = month>0? month+"月":"";
  985. String d = day>0? day+"天":"";
  986. return y+m+d;
  987. }
  988. public static int getWeekOfMonth(String dateString){
  989. Date date = strToDate(dateString);
  990. return getWeekOfMonth(date);
  991. }
  992. public static int getWeekOfMonth(Date date){
  993. Calendar calendar = Calendar.getInstance();
  994. calendar.setTime(date);
  995. int weekOfMonth = calendar.get(Calendar.WEEK_OF_MONTH);
  996. return weekOfMonth;
  997. }
  998. //将时间维度查出来的quotaDate转成yyyy-MM
  999. public static String changeQuotaDate(Date quotaDate){
  1000. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
  1001. String date = sdf.format(quotaDate);
  1002. return date;
  1003. }
  1004. }