1、日期格式化-工具类-One

  1. /**
  2. * 日期工具类
  3. */
  4. public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
  5. private static String[] parsePatterns = {
  6. "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
  7. "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
  8. "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};
  9. /**
  10. * 得到当前日期字符串 格式(yyyy-MM-dd)
  11. */
  12. public static String getDate() {
  13. return getDate("yyyy-MM-dd");
  14. }
  15. /**
  16. * 得到当前日期字符串 格式(yyyy-MM-dd) pattern可以为:"yyyy-MM-dd" "HH:mm:ss" "E"
  17. */
  18. public static String getDate(String pattern) {
  19. return DateFormatUtils.format(new Date(), pattern);
  20. }
  21. /**
  22. * 得到日期字符串 默认格式(yyyy-MM-dd) pattern可以为:"yyyy-MM-dd" "HH:mm:ss" "E"
  23. */
  24. public static String formatDate(Date date, Object... pattern) {
  25. String formatDate = null;
  26. if (pattern != null && pattern.length > 0) {
  27. formatDate = DateFormatUtils.format(date, pattern[0].toString());
  28. } else {
  29. formatDate = DateFormatUtils.format(date, "yyyy-MM-dd");
  30. }
  31. return formatDate;
  32. }
  33. /**
  34. * 字符串转日期
  35. * 默认格式(yyyy-MM-dd HH:mm:ss) pattern可以为:"yyyy-MM-dd" "HH:mm:ss"
  36. */
  37. public static Date stringToDate(String string, String pattern) {
  38. SimpleDateFormat sdf = null;
  39. Date date = null;
  40. try {
  41. if (pattern != null && pattern.length() > 0) {
  42. sdf = new SimpleDateFormat(pattern);
  43. date = sdf.parse(string);
  44. } else {
  45. sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  46. date = sdf.parse(string);
  47. }
  48. } catch (ParseException e) {
  49. e.printStackTrace();
  50. }
  51. return date;
  52. }
  53. /**
  54. * 得到日期时间字符串,转换格式(yyyy-MM-dd HH:mm:ss)
  55. */
  56. public static String formatDateTime(Date date) {
  57. return formatDate(date, "yyyy-MM-dd HH:mm:ss");
  58. }
  59. /**
  60. * 得到当前时间字符串 格式(HH:mm:ss)
  61. */
  62. public static String getTime() {
  63. return formatDate(new Date(), "HH:mm:ss");
  64. }
  65. /**
  66. * 得到当前日期和时间字符串 格式(yyyy-MM-dd HH:mm:ss)
  67. */
  68. public static String getDateTime() {
  69. return formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
  70. }
  71. /**
  72. * 得到当前年份字符串 格式(yyyy)
  73. */
  74. public static String getYear() {
  75. return formatDate(new Date(), "yyyy");
  76. }
  77. /**
  78. * 得到当前月份字符串 格式(MM)
  79. */
  80. public static String getMonth() {
  81. return formatDate(new Date(), "MM");
  82. }
  83. /**
  84. * 得到当天字符串 格式(dd)
  85. */
  86. public static String getDay() {
  87. return formatDate(new Date(), "dd");
  88. }
  89. /**
  90. * 得到当前星期字符串 格式(E)星期几
  91. */
  92. public static String getWeek() {
  93. return formatDate(new Date(), "E");
  94. }
  95. /**
  96. * 得到当前星期字符串 格式(E)星期几
  97. *
  98. * @param string
  99. */
  100. public static String getWeek(String string) {
  101. return formatDate(stringToDate(string, ""), "E");
  102. }
  103. /**
  104. * 日期型字符串转化为日期 格式
  105. * { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm",
  106. * "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm",
  107. * "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm" }
  108. */
  109. public static Date parseDate(Object str) {
  110. if (str == null) {
  111. return null;
  112. }
  113. try {
  114. return parseDate(str.toString(), parsePatterns);
  115. } catch (ParseException e) {
  116. return null;
  117. }
  118. }
  119. /**
  120. * 获取过去的天数
  121. *
  122. * @param date
  123. * @return
  124. */
  125. public static long pastDays(Date date) {
  126. long t = System.currentTimeMillis() - date.getTime();
  127. return t / (24 * 60 * 60 * 1000);
  128. }
  129. /**
  130. * 获取过去的小时
  131. *
  132. * @param date
  133. * @return
  134. */
  135. public static long pastHour(Date date) {
  136. long t = System.currentTimeMillis() - date.getTime();
  137. return t / (60 * 60 * 1000);
  138. }
  139. /**
  140. * 获取过去的分钟
  141. *
  142. * @param date
  143. * @return
  144. */
  145. public static long pastMinutes(Date date) {
  146. long t = System.currentTimeMillis() - date.getTime();
  147. return t / (60 * 1000);
  148. }
  149. /**
  150. * 转换为时间(天,时:分:秒.毫秒)
  151. *
  152. * @param timeMillis
  153. * @return
  154. */
  155. public static String formatDateTime(long timeMillis) {
  156. long day = timeMillis / (24 * 60 * 60 * 1000);
  157. long hour = (timeMillis / (60 * 60 * 1000) - day * 24);
  158. long min = ((timeMillis / (60 * 1000)) - day * 24 * 60 - hour * 60);
  159. long s = (timeMillis / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
  160. long sss = (timeMillis - day * 24 * 60 * 60 * 1000 - hour * 60 * 60 * 1000 - min * 60 * 1000 - s * 1000);
  161. return (day > 0 ? day + "," : "") + hour + ":" + min + ":" + s + "." + sss;
  162. }
  163. /**
  164. * 获取两个日期之间的天数
  165. *
  166. * @param before
  167. * @param after
  168. * @return
  169. */
  170. public static double getDistanceOfTwoDate(Date before, Date after) {
  171. long beforeTime = before.getTime();
  172. long afterTime = after.getTime();
  173. return (afterTime - beforeTime) / (1000 * 60 * 60 * 24);
  174. }
  175. /**
  176. * 获取当前日期前一天日期
  177. *
  178. * @return
  179. */
  180. public static String getBeforeDate() {
  181. Calendar calendar = Calendar.getInstance(); //得到日历
  182. calendar.setTime(new Date());//把当前时间赋给日历
  183. calendar.add(Calendar.DAY_OF_MONTH, -1); //设置为前一天
  184. Date dBefore = calendar.getTime(); //得到前一天的时间
  185. SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); //设置时间格式
  186. String beforeDate = sdf.format(dBefore); //格式化前一天
  187. return beforeDate;
  188. }
  189. /**
  190. * @param day 获取当前日期前后day天日期,day>0后几天,day<0前几天
  191. * @param curdate 当前日期
  192. * @return
  193. */
  194. public static String getAfterDate(int day, String curdate, String dateFormat) {
  195. Calendar calendar = Calendar.getInstance();
  196. calendar.setTime(stringToDate(curdate, dateFormat));
  197. calendar.add(Calendar.DAY_OF_MONTH, day);
  198. Date dAfter = calendar.getTime();
  199. SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
  200. String afterDate = sdf.format(dAfter);
  201. return afterDate;
  202. }
  203. /**
  204. * @param day 获取当前日期前后day天日期,day>0后几天,day<0前几天
  205. * @return
  206. */
  207. public static String getAfterDate(int day, String dateFormat) {
  208. Calendar calendar = Calendar.getInstance();
  209. calendar.setTime(new Date());
  210. calendar.add(Calendar.DAY_OF_MONTH, day);
  211. Date dAfter = calendar.getTime();
  212. SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
  213. String afterDate = sdf.format(dAfter);
  214. return afterDate;
  215. }
  216. public static Date getThisWeekMonday(Date date) {
  217. Calendar cal = Calendar.getInstance();
  218. cal.setTime(date);
  219. // 获得当前日期是一个星期的第几天
  220. int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
  221. if (1 == dayWeek) {
  222. cal.add(Calendar.DAY_OF_MONTH, -1);
  223. }
  224. // 设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
  225. cal.setFirstDayOfWeek(Calendar.MONDAY);
  226. // 获得当前日期是一个星期的第几天
  227. int day = cal.get(Calendar.DAY_OF_WEEK);
  228. // 根据日历的规则,给当前日期减去星期几与一个星期第一天的差值
  229. cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
  230. return cal.getTime();
  231. }
  232. /**
  233. * 获取本周一
  234. *
  235. * @param dateFormat
  236. * @return
  237. */
  238. public static String getThisWeekMonday(String dateFormat) {
  239. Calendar cal = Calendar.getInstance();
  240. cal.setTime(new Date());
  241. // 获得当前日期是一个星期的第几天
  242. int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
  243. if (1 == dayWeek) {
  244. cal.add(Calendar.DAY_OF_MONTH, -1);
  245. }
  246. // 设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
  247. cal.setFirstDayOfWeek(Calendar.MONDAY);
  248. // 获得当前日期是一个星期的第几天
  249. int day = cal.get(Calendar.DAY_OF_WEEK);
  250. // 根据日历的规则,给当前日期减去星期几与一个星期第一天的差值
  251. cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
  252. SimpleDateFormat sdf = new SimpleDateFormat(dateFormat); //设置时间格式
  253. return sdf.format(cal.getTime());
  254. }
  255. /**
  256. * 获取下周一
  257. *
  258. * @param dateFormat
  259. * @return
  260. */
  261. public static String getNextWeekMonday(String dateFormat) {
  262. Calendar cal = Calendar.getInstance();
  263. cal.setTime(getThisWeekMonday(new Date()));
  264. cal.add(Calendar.DATE, 7);
  265. SimpleDateFormat sdf = new SimpleDateFormat(dateFormat); //设置时间格式
  266. return sdf.format(cal.getTime());
  267. }
  268. /**
  269. * 获取下月
  270. *
  271. * @return
  272. */
  273. public static String getNextMonth() {
  274. Calendar c = Calendar.getInstance();
  275. c.add(Calendar.MONTH, +1);//下个月
  276. String year = String.valueOf(c.get(Calendar.YEAR));
  277. //下个月
  278. String nextMonth = String.valueOf(c.get(Calendar.MONTH) + 1).length() == 2 ? String.valueOf(c.get(Calendar.MONTH) + 1) : "0" + String.valueOf(c.get(Calendar.MONTH) + 1);
  279. return year + "-" + nextMonth;
  280. }
  281. /**
  282. * 获取本月
  283. *
  284. * @return
  285. */
  286. public static String getThisMonth() {
  287. Calendar c = Calendar.getInstance();
  288. c.add(Calendar.MONTH, 0);//本月
  289. String year = String.valueOf(c.get(Calendar.YEAR));
  290. //本月
  291. String thisMonth = String.valueOf(c.get(Calendar.MONTH) + 1).length() == 2 ? String.valueOf(c.get(Calendar.MONTH) + 1) : "0" + String.valueOf(c.get(Calendar.MONTH) + 1);
  292. return year + "-" + thisMonth;
  293. }
  294. /**
  295. * 当前季度的开始时间
  296. *
  297. * @return
  298. */
  299. public static String getCurrentQuarterStartTime(String dateFormat) {
  300. SimpleDateFormat sdf = new SimpleDateFormat(dateFormat); //设置时间格式
  301. Calendar c = Calendar.getInstance();
  302. int currentMonth = c.get(Calendar.MONTH);
  303. try {
  304. if (currentMonth >= 1 && currentMonth <= 3) {
  305. c.set(Calendar.MONTH, 0);
  306. } else if (currentMonth >= 4 && currentMonth <= 6) {
  307. c.set(Calendar.MONTH, 3);
  308. } else if (currentMonth >= 7 && currentMonth <= 9) {
  309. c.set(Calendar.MONTH, 6);
  310. } else if (currentMonth >= 10 && currentMonth <= 12) {
  311. c.set(Calendar.MONTH, 9);
  312. c.set(Calendar.DATE, 1);
  313. }
  314. } catch (Exception e) {
  315. e.printStackTrace();
  316. }
  317. return sdf.format(c.getTime());
  318. }
  319. /**
  320. * 当前季度的结束时间
  321. *
  322. * @return
  323. */
  324. public static String getCurrentQuarterEndDate(String dateFormat) {
  325. SimpleDateFormat sdf = new SimpleDateFormat(dateFormat); //设置时间格式
  326. Calendar c = Calendar.getInstance();
  327. int currentMonth = c.get(Calendar.MONTH) + 1;
  328. try {
  329. if (currentMonth >= 1 && currentMonth <= 3) {
  330. c.set(Calendar.MONTH, 2);
  331. c.set(Calendar.DATE, 31);
  332. } else if (currentMonth >= 4 && currentMonth <= 6) {
  333. c.set(Calendar.MONTH, 5);
  334. c.set(Calendar.DATE, 30);
  335. } else if (currentMonth >= 7 && currentMonth <= 9) {
  336. c.set(Calendar.MONTH, 8);
  337. c.set(Calendar.DATE, 30);
  338. } else if (currentMonth >= 10 && currentMonth <= 12) {
  339. c.set(Calendar.MONTH, 11);
  340. c.set(Calendar.DATE, 31);
  341. }
  342. } catch (Exception e) {
  343. e.printStackTrace();
  344. }
  345. c.setTime(new Date());
  346. return sdf.format(c.getTime());
  347. }
  348. /**
  349. * 当前季度的结束时间
  350. *
  351. * @return
  352. */
  353. public static String getCurrentQuarterEndDate(String dateFormat, String curMonth) {
  354. SimpleDateFormat sdf = new SimpleDateFormat(dateFormat); //设置时间格式
  355. Calendar c = Calendar.getInstance();
  356. int currentMonth = c.get(Calendar.MONTH) + 1;
  357. try {
  358. if (currentMonth >= 1 && currentMonth <= 3) {
  359. c.set(Calendar.MONTH, 2);
  360. c.set(Calendar.DATE, 31);
  361. } else if (currentMonth >= 4 && currentMonth <= 6) {
  362. c.set(Calendar.MONTH, 5);
  363. c.set(Calendar.DATE, 30);
  364. } else if (currentMonth >= 7 && currentMonth <= 9) {
  365. c.set(Calendar.MONTH, 8);
  366. c.set(Calendar.DATE, 30);
  367. } else if (currentMonth >= 10 && currentMonth <= 12) {
  368. c.set(Calendar.MONTH, 11);
  369. c.set(Calendar.DATE, 31);
  370. }
  371. } catch (Exception e) {
  372. e.printStackTrace();
  373. }
  374. c.setTime(stringToDate(curMonth, dateFormat));
  375. return sdf.format(c.getTime());
  376. }
  377. /**
  378. * 获取月份
  379. *
  380. * @param -month 前month月,i后month月
  381. * @param dateFormat 日期格式
  382. * @return
  383. */
  384. public static String getMonth(int month, String dateFormat) {
  385. SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
  386. Calendar c = Calendar.getInstance();
  387. c.setTime(new Date());
  388. c.add(Calendar.MONTH, month);
  389. Date m = c.getTime();
  390. return sdf.format(m);
  391. }
  392. /**
  393. * 获取月份
  394. *
  395. * @param -month 前month月,month后month月
  396. * @param dateFormat 日期格式 yyyyMM
  397. * @param curMonth
  398. * @return
  399. */
  400. public static String getMonth(int month, String curMonth, String dateFormat) {
  401. SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
  402. Calendar c = Calendar.getInstance();
  403. c.setTime(stringToDate(curMonth, dateFormat));
  404. c.add(Calendar.MONTH, month);
  405. Date m = c.getTime();
  406. return sdf.format(m);
  407. }
  408. /**
  409. * 获取前minute分钟时间:如-4,后minute分钟时间:如 4
  410. *
  411. * @param minute 格式 yyyy-MM-dd HH:mm:ss
  412. * @return
  413. */
  414. public static String getBeforeMinute(int minute) {
  415. SimpleDateFormat sdf = new SimpleDateFormat(parsePatterns[1]);
  416. Calendar beforeTime = Calendar.getInstance();
  417. beforeTime.add(Calendar.MINUTE, minute);
  418. Date beforeD = beforeTime.getTime();
  419. String time = sdf.format(beforeD);
  420. return time;
  421. }
  422. public static Date getIntervalMinute(int minute) {
  423. SimpleDateFormat sdf = new SimpleDateFormat(parsePatterns[1]);
  424. Calendar beforeTime = Calendar.getInstance();
  425. beforeTime.add(Calendar.MINUTE, minute);
  426. Date beforeD = beforeTime.getTime();
  427. return beforeD;
  428. }
  429. /**
  430. * 获取前minute分钟:如-4,后minute分钟:如 4
  431. * @param minute
  432. * @return
  433. */
  434. public static int getMinute(int minute){
  435. String dataTime = getBeforeMinute(minute);
  436. String[] time = dataTime.split(" ")[1].split(":");
  437. //时间转为分钟数
  438. int mi = Integer.valueOf(time[0])*60 + Integer.valueOf(time[1]);
  439. return mi;
  440. }
  441. /**
  442. * 获取前minute分钟时间:如-4,后minute分钟时间:如 4
  443. *
  444. * @param curDateTime 当前时间
  445. * @param minute 间隔分钟
  446. * 格式 yyyy-MM-dd HH:mm:ss
  447. * @return
  448. */
  449. public static String getBeforeMinute(String curDateTime, int minute) {
  450. SimpleDateFormat sdf = new SimpleDateFormat(parsePatterns[1]);
  451. Calendar beforeTime = Calendar.getInstance();
  452. beforeTime.setTime(stringToDate(curDateTime, null));
  453. beforeTime.add(Calendar.MINUTE, minute);
  454. Date beforeD = beforeTime.getTime();
  455. String time = sdf.format(beforeD);
  456. return time;
  457. }
  458. /**
  459. * 获取前hour小时:如-4,后hour小时:如 4
  460. *
  461. * @param hour 格式 yyyy-MM-dd HH:mm:ss
  462. * @return
  463. */
  464. public static String getBeforeHour(int hour) {
  465. SimpleDateFormat sdf = new SimpleDateFormat(parsePatterns[1]);
  466. Calendar beforeTime = Calendar.getInstance();
  467. beforeTime.add(Calendar.HOUR_OF_DAY, hour);
  468. Date beforeD = beforeTime.getTime();
  469. String time = sdf.format(beforeD);
  470. return time;
  471. }
  472. /**
  473. * 获取前second秒:如-4,后second秒:如 4
  474. *
  475. * @param second 格式 yyyy-MM-dd HH:mm:ss
  476. * @return
  477. */
  478. public static String getBeforeSecond(int second) {
  479. SimpleDateFormat sdf = new SimpleDateFormat(parsePatterns[1]);
  480. Calendar beforeTime = Calendar.getInstance();
  481. beforeTime.add(Calendar.SECOND, second);
  482. Date beforeD = beforeTime.getTime();
  483. String time = sdf.format(beforeD);
  484. return time;
  485. }
  486. /**
  487. * @param date1 字符串日期1
  488. * @param date2 字符串日期2
  489. * @param pattern 日期格式化方式
  490. * @return
  491. * @descript:计算两个字符串月相差的月数
  492. */
  493. public static int countMonths(String date1, String date2, String pattern) {
  494. SimpleDateFormat sdf = new SimpleDateFormat(pattern);
  495. Calendar c1 = Calendar.getInstance();
  496. Calendar c2 = Calendar.getInstance();
  497. try {
  498. c1.setTime(sdf.parse(date1));
  499. c2.setTime(sdf.parse(date2));
  500. } catch (ParseException e) {
  501. e.printStackTrace();
  502. }
  503. int year = c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR);
  504. //开始日期若小月结束日期
  505. if (year < 0) {
  506. year = -year;
  507. return year * 12 + c1.get(Calendar.MONTH) - c2.get(Calendar.MONTH);
  508. }
  509. return year * 12 + c2.get(Calendar.MONTH) - c1.get(Calendar.MONTH);
  510. }
  511. /**
  512. * 获取当前时间前几天,当前天,后几天日期yyyyMMdd
  513. *
  514. * @param day 和当期天的间隔天数 day前几天,-day:前day天,day:后day天
  515. * @param dateFormat 时间格式
  516. * @return
  517. */
  518. public static String getDay(int day, String dateFormat) {
  519. Calendar calendar = Calendar.getInstance();
  520. calendar.add(Calendar.DATE, day);
  521. Date date = calendar.getTime();
  522. SimpleDateFormat df = new SimpleDateFormat(dateFormat);
  523. return df.format(date);
  524. }
  525. /**
  526. * 获取当前时间前几天,当前天,后几天日期yyyyMMdd
  527. *
  528. * @param day 和当期天的间隔天数 day前几天,-day:前day天,day:后day天
  529. * @param dateFormat 时间格式
  530. * @return
  531. */
  532. public static String getDay(int day, String dateFormat, String curDay) {
  533. Calendar c = Calendar.getInstance();
  534. c.setTime(stringToDate(curDay, dateFormat));
  535. c.add(Calendar.DATE, day);
  536. Date date = c.getTime();
  537. SimpleDateFormat df = new SimpleDateFormat(dateFormat);
  538. return df.format(date);
  539. }
  540. /**
  541. * 获取当前时间小时
  542. *
  543. */
  544. public static String getHour() {
  545. GregorianCalendar calendar = new GregorianCalendar();
  546. int hour = calendar.get(Calendar.HOUR_OF_DAY);
  547. return hour + "";
  548. }
  549. /**
  550. * 星期表达式转换
  551. *
  552. * @param weekType
  553. * @return
  554. */
  555. public static int getNumWeek(String weekType) {
  556. int week = 0;
  557. switch (weekType) {
  558. case "星期一":
  559. week = 1;
  560. break;
  561. case "星期二":
  562. week = 2;
  563. break;
  564. case "星期三":
  565. week = 3;
  566. break;
  567. case "星期四":
  568. week = 4;
  569. break;
  570. case "星期五":
  571. week = 5;
  572. break;
  573. case "星期六":
  574. week = 6;
  575. break;
  576. case "星期日":
  577. week = 7;
  578. break;
  579. default:
  580. }
  581. return week;
  582. }
  583. /**
  584. * 星期表达式转换
  585. *
  586. * @param weekNum
  587. * @return
  588. */
  589. public static String getWeekByNum(Integer weekNum) {
  590. String week = "";
  591. switch (weekNum) {
  592. case 1:
  593. week = "星期一";
  594. break;
  595. case 2:
  596. week = "星期二";
  597. break;
  598. case 3:
  599. week = "星期三";
  600. break;
  601. case 4:
  602. week = "星期四";
  603. break;
  604. case 5:
  605. week = "星期五";
  606. break;
  607. case 6:
  608. week = "星期六";
  609. break;
  610. case 7:
  611. week = "星期日";
  612. break;
  613. default:
  614. }
  615. return week;
  616. }
  617. /**
  618. * @param week 星期类型 :例如:星期一、星期二、星期三、星期四、星期五、星期六、星期日
  619. * @param dateFormat 返回的日期格式
  620. * @return
  621. */
  622. public static String getDayByWeek(String week, String dateFormat) {
  623. int numWeek1 = getNumWeek(getWeek());
  624. int numWeek2 = getNumWeek(week);
  625. int weekdiff = numWeek2 - numWeek1;
  626. return getDay(weekdiff, dateFormat);
  627. }
  628. /**
  629. * @param week 星期类型 :例如:星期一、星期二、星期三、星期四、星期五、星期六、星期日
  630. * @param dateFormat 返回的日期格式
  631. * @return
  632. */
  633. public static String getDayByWeek(String week, String dateFormat, String curDay) {
  634. int numWeek1 = getNumWeek(getWeek(curDay));
  635. int numWeek2 = getNumWeek(week);
  636. int weekdiff = numWeek2 - numWeek1;
  637. return getDay(weekdiff, dateFormat, curDay);
  638. }
  639. /**
  640. * 当前日期所在周的前一周
  641. * 例如:201907
  642. *
  643. * @return
  644. * @throws ParseException
  645. */
  646. public static String getWeekAndYear(int day) {
  647. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  648. Date date = null;
  649. try {
  650. date = format.parse(getAfterDate(day, "yyyy-MM-dd"));
  651. } catch (ParseException e) {
  652. e.getMessage();
  653. }
  654. Calendar cal = Calendar.getInstance();
  655. //设置周一为一周的第一天
  656. cal.setFirstDayOfWeek(Calendar.MONDAY);
  657. cal.setTime(date);
  658. int week = cal.get(Calendar.WEEK_OF_YEAR);
  659. int year = cal.get(Calendar.YEAR);
  660. int month = cal.get(Calendar.MONTH);
  661. //如果月份是12月,且求出来的周数是第一周,说明该日期实质上是这一年的第53周,也是下一年的第一周
  662. if(month>=11 && week <= 1){
  663. week +=52;
  664. }
  665. String yearWeek = "";
  666. if (week < 10) {
  667. yearWeek = year + "0" + week;
  668. } else {
  669. yearWeek = year + "" + week;
  670. }
  671. return yearWeek;
  672. }
  673. /**
  674. * 当前日期所在周的前一周
  675. * 例如:201907
  676. *
  677. * @return
  678. * @throws ParseException
  679. */
  680. public static String getWeekAndYear(int day, String curWeek) {
  681. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  682. Date date = null;
  683. try {
  684. date = format.parse(getAfterDate(day, "yyyy-MM-dd"));
  685. } catch (ParseException e) {
  686. e.getMessage();
  687. }
  688. Calendar cal = Calendar.getInstance();
  689. //设置周一为一周的第一天
  690. cal.setFirstDayOfWeek(Calendar.MONDAY);
  691. cal.setTime(date);
  692. int week = cal.get(Calendar.WEEK_OF_YEAR);
  693. int year = cal.get(Calendar.YEAR);
  694. int month = cal.get(Calendar.MONTH);
  695. //如果月份是12月,且求出来的周数是第一周,说明该日期实质上是这一年的第53周,也是下一年的第一周
  696. if(month>=11 && week <= 1){
  697. week +=52;
  698. }
  699. String yearWeek = "";
  700. if (week < 10) {
  701. yearWeek = year + "0" + week;
  702. } else {
  703. yearWeek = year + "" + week;
  704. }
  705. return yearWeek;
  706. }
  707. /**
  708. * 通过日期获取周数
  709. * 格式:yyyy-MM-dd
  710. * @param day
  711. * @return
  712. */
  713. public static String getWeekAndYear(String day, String pattern) {
  714. SimpleDateFormat format = new SimpleDateFormat(pattern);
  715. Date date = null;
  716. try {
  717. date = format.parse(day);
  718. } catch (ParseException e) {
  719. e.getMessage();
  720. }
  721. Calendar cal = Calendar.getInstance();
  722. //设置周一为一周的第一天
  723. cal.setFirstDayOfWeek(Calendar.MONDAY);
  724. cal.setTime(date);
  725. int week = cal.get(Calendar.WEEK_OF_YEAR);
  726. int year = cal.get(Calendar.YEAR);
  727. int month = cal.get(Calendar.MONTH);
  728. //如果月份是12月,且求出来的周数是第一周,说明该日期实质上是这一年的第53周,也是下一年的第一周
  729. if(month>=11 && week <= 1){
  730. week +=52;
  731. }
  732. String yearWeek = "";
  733. if (week < 10) {
  734. yearWeek = year + "0" + week;
  735. } else {
  736. yearWeek = year + "" + week;
  737. }
  738. return yearWeek;
  739. }
  740. /**
  741. * 获取日期所在周
  742. *
  743. * @param curDate 格式:yyyyMMdd
  744. * @return
  745. */
  746. public static String getWeekByDate(String curDate) {
  747. SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
  748. Date date = null;
  749. try {
  750. date = format.parse(curDate);
  751. } catch (ParseException e) {
  752. e.printStackTrace();
  753. }
  754. Calendar calendar = Calendar.getInstance();
  755. calendar.setFirstDayOfWeek(Calendar.MONDAY);
  756. calendar.setTime(date);
  757. int week = calendar.get(Calendar.WEEK_OF_YEAR);
  758. int year = calendar.getWeekYear();
  759. String yearWeek = "";
  760. if(week <10){
  761. yearWeek = year + "0"+ week;
  762. }else{
  763. yearWeek = year + ""+ week;
  764. }
  765. return yearWeek;
  766. }
  767. /**
  768. * 增加半小时
  769. * @param day
  770. * @param min
  771. * @return
  772. */
  773. public static String addHalfHour(String day, int min){
  774. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  775. Date date = null;
  776. try {
  777. date = format.parse(day);
  778. } catch (Exception ex) {
  779. ex.printStackTrace();
  780. }
  781. if (date == null) {
  782. return "";
  783. }
  784. Calendar cal = Calendar.getInstance();
  785. cal.setTime(date);
  786. // 24小时制
  787. cal.add(Calendar.MINUTE, min);
  788. date = cal.getTime();
  789. return format.format(date);
  790. }
  791. /**
  792. * 窗口时间换算成毫秒数
  793. * @param windowTime
  794. * @return
  795. */
  796. public static Long getWindowMillis(String windowTime){
  797. long windowTimeSecond = -1;
  798. String hour = "小时";
  799. String minute = "分钟";
  800. String second = "秒";
  801. if (windowTime == null || windowTime.trim().length() == 0) {
  802. return windowTimeSecond;
  803. }
  804. windowTime = windowTime.replaceAll(" ", "");
  805. if (windowTime.indexOf(second) > 0) {
  806. windowTime = windowTime.replace(second, "").trim();
  807. windowTimeSecond = Integer.parseInt(windowTime) * 1000;
  808. } else if (windowTime.indexOf(minute) > 0) {
  809. windowTime = windowTime.replace(minute, "").trim();
  810. windowTimeSecond = Integer.parseInt(windowTime) * 60 * 1000;
  811. } else if (windowTime.indexOf(hour) > 0) {
  812. windowTime = windowTime.replace(hour, "").trim();
  813. windowTimeSecond = Integer.parseInt(windowTime) * 60 * 60 * 1000;
  814. }
  815. return windowTimeSecond;
  816. }
  817. /**
  818. * 获取所在周开始时间
  819. * @param weekYear 年 2020
  820. * @param weekOfYear 周 12
  821. * @param pattern 日期格式
  822. * @return
  823. */
  824. public static String getFirstDayOfWeek(int weekYear, int weekOfYear, String pattern){
  825. Calendar calendar = Calendar.getInstance();
  826. //设置星期一为一周开始的第一天
  827. calendar.setFirstDayOfWeek(Calendar.MONDAY);
  828. //可以不用设置
  829. calendar.setMinimalDaysInFirstWeek(4);
  830. calendar.setTimeInMillis(System.currentTimeMillis());
  831. //获得指定年的第几周的开始日期
  832. calendar.setWeekDate(weekYear, weekOfYear, 2);
  833. //创建日期的时间该周的第一天
  834. long starttime = calendar.getTime().getTime();
  835. SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
  836. //将时间戳格式化为指定格式
  837. String dateStart = simpleDateFormat.format(starttime);
  838. dateStart = getAfterDate(-7, dateStart, pattern);
  839. return dateStart;
  840. }
  841. /**
  842. * 获取指定周前几周
  843. * @param curWeek 当前周 202012
  844. * @param num 前后几周
  845. * @param pattern 日期格式
  846. * @return
  847. */
  848. public static String getAfterWeek(String curWeek,int num, String pattern){
  849. //年
  850. int weekYear = Integer.valueOf(curWeek.substring(0,4));
  851. //周数
  852. int weekOfYear = Integer.valueOf(curWeek.substring(4,6));
  853. //周对应的开始日期
  854. String weekDate = getFirstDayOfWeek(weekYear, weekOfYear, pattern);
  855. //获取前后几天
  856. String afterDate = getAfterDate(num*7, weekDate, pattern);
  857. String yearAndWeek = getWeekAndYear(afterDate,pattern);
  858. //年
  859. int weekYear1 = Integer.valueOf(yearAndWeek.substring(0,4));
  860. //当前年和前一年同时存在,取当前年第一周
  861. if(weekYear > weekYear1){
  862. yearAndWeek = weekYear+"01";
  863. }
  864. return yearAndWeek;
  865. }
  866. /**
  867. * 计算相差的天数
  868. * @param startDate
  869. * @param endDate
  870. * @return
  871. * @throws ParseException
  872. */
  873. public static int getDayDiffer(Date startDate, Date endDate) {
  874. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
  875. long startDateTime = 0;
  876. try {
  877. startDateTime = dateFormat.parse(dateFormat.format(startDate)).getTime();
  878. long endDateTime = dateFormat.parse(dateFormat.format(endDate)).getTime();
  879. return (int) ((endDateTime - startDateTime) / (1000 * 3600 * 24));
  880. } catch (ParseException e) {
  881. e.printStackTrace();
  882. }
  883. return 0;
  884. }
  885. public static int getMiniteDiffer(Date startDate, Date endDate) {
  886. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  887. long startDateTime = 0;
  888. try {
  889. startDateTime = dateFormat.parse(dateFormat.format(startDate)).getTime();
  890. long endDateTime = dateFormat.parse(dateFormat.format(endDate)).getTime();
  891. return (int) (endDateTime - startDateTime) / (1000 * 60);
  892. } catch (ParseException e) {
  893. e.printStackTrace();
  894. }
  895. return 0;
  896. }
  897. /**
  898. * 获取指定时间diff小时后的数据
  899. * @param strDate
  900. * @param pattern
  901. * @param diff
  902. * @return
  903. */
  904. public static String getHourDiffer(String strDate, String pattern, int diff) {
  905. Date date = DateUtils.stringToDate(strDate, pattern);
  906. Calendar rightNow = Calendar.getInstance();
  907. rightNow.setTime(date);
  908. rightNow.add(Calendar.HOUR, diff);
  909. rightNow.add(Calendar.MINUTE, -5);
  910. Date time = rightNow.getTime();
  911. String times = DateUtils.formatDateTime(time);
  912. return times;
  913. }
  914. }