1. 程式人生 > >jdk8時間工具類

jdk8時間工具類

 

 
  1. package com.changfu.common.util;

  2.  
  3. import org.slf4j.Logger;

  4. import org.slf4j.LoggerFactory;

  5.  
  6. import java.sql.Timestamp;

  7. import java.time.*;

  8. import java.time.format.DateTimeFormatter;

  9. import java.time.format.DateTimeParseException;

  10. import java.time.temporal.ChronoUnit;

  11. import java.util.Calendar;

  12. import java.util.Date;

  13. import java.util.GregorianCalendar;

  14.  
  15.  
  16. /**

  17. * 時間計算工具類

  18. */

  19. public class DateUtil {

  20.  
  21. /**

  22. * the milli second of a day

  23. */

  24. public static final long DAYMILLI = 24 * 60 * 60 * 1000;

  25. /**

  26. * the milli seconds of an hour

  27. */

  28. public static final long HOURMILLI = 60 * 60 * 1000;

  29. /**

  30. * the milli seconds of a minute

  31. */

  32. public static final long MINUTEMILLI = 60 * 1000;

  33. /**

  34. * the milli seconds of a second

  35. */

  36. public static final long SECONDMILLI = 1000;

  37. /**

  38. * added time

  39. */

  40. public static final String TIMETO = " 23:59:59";

  41. /**

  42. * flag before

  43. */

  44. public static final transient int BEFORE = 1;

  45. /**

  46. * flag after

  47. */

  48. public static final transient int AFTER = 2;

  49. /**

  50. * flag equal

  51. */

  52. public static final transient int EQUAL = 3;

  53. /**

  54. * date format dd/MMM/yyyy:HH:mm:ss +0900

  55. */

  56. public static final String TIME_PATTERN_LONG = "dd/MMM/yyyy:HH:mm:ss +0900";

  57. /**

  58. * date format dd/MM/yyyy:HH:mm:ss +0900

  59. */

  60. public static final String TIME_PATTERN_LONG2 = "dd/MM/yyyy:HH:mm:ss +0900";

  61. /**

  62. * date format YYYY-MM-DD HH24:MI:SS

  63. */

  64. public static final String DB_TIME_PATTERN = "YYYY-MM-DD HH24:MI:SS";

  65. /**

  66. * date format YYYYMMDDHH24MISS

  67. */

  68. public static final String DB_TIME_PATTERN_1 = "YYYYMMDDHH24MISS";

  69. /**

  70. * date format dd/MM/yy HH:mm:ss

  71. */

  72. public static final String TIME_PATTERN_SHORT = "dd/MM/yy HH:mm:ss";

  73. /**

  74. * date format dd/MM/yy HH24:mm

  75. */

  76. public static final String TIME_PATTERN_SHORT_1 = "yyyy/MM/dd HH:mm";

  77. /**

  78. * date format yyyy年MM月dd日 HH:mm:ss

  79. */

  80. public static final String TIME_PATTERN_SHORT_2 = "yyyy年MM月dd日 HH:mm:ss";

  81. /**

  82. * date format yyyyMMddHHmmss

  83. */

  84. public static final String TIME_PATTERN_SESSION = "yyyyMMddHHmmss";

  85. /**

  86. * date format yyyyMMddHHmmssSSS

  87. */

  88. public static final String TIME_PATTERN_MILLISECOND = "yyyyMMddHHmmssSSS";

  89. /**

  90. * date format yyyyMMdd

  91. */

  92. public static final String DATE_FMT_0 = "yyyyMMdd";

  93. /**

  94. * date format yyyy/MM/dd

  95. */

  96. public static final String DATE_FMT_1 = "yyyy/MM/dd";

  97. /**

  98. * date format yyyy/MM/dd hh:mm:ss

  99. */

  100. public static final String DATE_FMT_2 = "yyyy/MM/dd hh:mm:ss";

  101. /**

  102. * date format yyyy-MM-dd

  103. */

  104. public static final String DATE_FMT_3 = "yyyy-MM-dd";

  105. /**

  106. * date format yyyy年MM月dd日

  107. */

  108. public static final String DATE_FMT_4 = "yyyy年MM月dd日";

  109. /**

  110. * date format yyyy-MM-dd HH

  111. */

  112. public static final String DATE_FMT_5 = "yyyy-MM-dd HH";

  113. /**

  114. * date format yyyy-MM

  115. */

  116. public static final String DATE_FMT_6 = "yyyy-MM";

  117. /**

  118. * date format MM月dd日 HH:mm

  119. */

  120. public static final String DATE_FMT_7 = "MM月dd日 HH:mm";

  121. /**

  122. * date format MM月dd日 HH:mm

  123. */

  124. public static final String DATE_FMT_8 = "HH:mm:ss";

  125. /**

  126. * date format MM月dd日 HH:mm

  127. */

  128. public static final String DATE_FMT_9 = "yyyy.MM.dd";

  129. public static final String DATE_FMT_10 = "HH:mm";

  130. public static final String DATE_FMT_11 = "yyyy.MM.dd HH:mm:ss";

  131. /**

  132. * date format yyyy年MM月dd日

  133. */

  134. public static final String DATE_FMT_12 = "MM月dd日";

  135. public static final String DATE_FMT_13 = "yyyy年MM月dd日HH時mm分";

  136. public static final String DATE_FMT_14 = "yyyyMM";

  137. public static final String DATE_FMT_15 = "MM-dd HH:mm:ss";

  138. public static final String DATE_FMT_16 = "yyyyMMddHHmm";

  139. public static final String DATE_FMT_17 = "HHmmss";

  140. public static final String DATE_FMT_18 = "yyyy";

  141. /**

  142. * date format yyyy-MM-dd HH:mm:ss

  143. */

  144. public static final String TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";

  145. private static final Logger logger = LoggerFactory.getLogger(DateUtil.class);

  146.  
  147. /**

  148. * localDateTime 轉 自定義格式string

  149. *

  150. * @param localDateTime

  151. * @param format 例:yyyy-MM-dd hh:mm:ss

  152. * @return

  153. */

  154. public static String formatLocalDateTimeToString(LocalDateTime localDateTime, String format) {

  155. try {

  156. DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);

  157. return localDateTime.format(formatter);

  158.  
  159. } catch (DateTimeParseException e) {

  160. e.printStackTrace();

  161. }

  162. return null;

  163. }

  164.  
  165. /**

  166. * string 轉 LocalDateTime

  167. *

  168. * @param dateStr 例:"2017-08-11 01:00:00"

  169. * @param format 例:"yyyy-MM-dd HH:mm:ss"

  170. * @return

  171. */

  172. public static LocalDateTime stringToLocalDateTime(String dateStr, String format) {

  173. try {

  174. DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);

  175. return LocalDateTime.parse(dateStr, formatter);

  176. } catch (DateTimeParseException e) {

  177. e.printStackTrace();

  178. }

  179. return null;

  180. }

  181.  
  182.  
  183. /**

  184. * 根據時間獲取當月有多少天數

  185. *

  186. * @param date

  187. * @return

  188. */

  189. public static int getActualMaximum(Date date) {

  190.  
  191. return dateToLocalDateTime(date).getMonth().length(dateToLocalDate(date).isLeapYear());

  192. }

  193.  
  194. /**

  195. * 根據日期獲得星期

  196. *

  197. * @param date

  198. * @return 1:星期一;2:星期二;3:星期三;4:星期四;5:星期五;6:星期六;7:星期日;

  199. */

  200. public static int getWeekOfDate(Date date) {

  201. return dateToLocalDateTime(date).getDayOfWeek().getValue();

  202. }

  203.  
  204.  
  205. /**

  206. * 計算兩個日期LocalDate相差的天數,不考慮日期前後,返回結果>=0

  207. *

  208. * @param before

  209. * @param after

  210. * @return

  211. */

  212. public static int getAbsDateDiffDay(LocalDate before, LocalDate after) {

  213.  
  214. return Math.abs(Period.between(before, after).getDays());

  215. }

  216.  
  217. /**

  218. * 計算兩個時間LocalDateTime相差的天數,不考慮日期前後,返回結果>=0

  219. *

  220. * @param before

  221. * @param after

  222. * @return

  223. */

  224. public static int getAbsTimeDiffDay(LocalDateTime before, LocalDateTime after) {

  225.  
  226. return Math.abs(Period.between(before.toLocalDate(), after.toLocalDate()).getDays());

  227. }

  228.  
  229. /**

  230. * 計算兩個時間LocalDateTime相差的月數,不考慮日期前後,返回結果>=0

  231. *

  232. * @param before

  233. * @param after

  234. * @return

  235. */

  236. public static int getAbsTimeDiffMonth(LocalDateTime before, LocalDateTime after) {

  237.  
  238. return Math.abs(Period.between(before.toLocalDate(), after.toLocalDate()).getMonths());

  239. }

  240.  
  241. /**

  242. * 計算兩個時間LocalDateTime相差的年數,不考慮日期前後,返回結果>=0

  243. *

  244. * @param before

  245. * @param after

  246. * @return

  247. */

  248. public static int getAbsTimeDiffYear(LocalDateTime before, LocalDateTime after) {

  249.  
  250. return Math.abs(Period.between(before.toLocalDate(), after.toLocalDate()).getYears());

  251. }

  252.  
  253.  
  254. /**

  255. * 根據傳入日期返回星期幾

  256. *

  257. * @param date 日期

  258. * @return 1-7 1:星期天,2:星期一,3:星期二,4:星期三,5:星期四,6:星期五,7:星期六

  259. */

  260. public static int getDayOfWeek(Date date) {

  261. Calendar cal = new GregorianCalendar();

  262. cal.setTime(date);

  263. return cal.get(Calendar.DAY_OF_WEEK);

  264. }

  265.  
  266.  
  267. /**

  268. * 獲取指定日期的當月的月份數

  269. *

  270. * @param date

  271. * @return

  272. */

  273. public static int getLastMonth(Date date) {

  274. return dateToLocalDateTime(date).getMonth().getValue();

  275.  
  276. }

  277.  
  278.  
  279. /**

  280. * 特定日期的當月第一天

  281. *

  282. * @param date

  283. * @return

  284. */

  285. public static LocalDate newThisMonth(Date date) {

  286. LocalDate localDate = dateToLocalDate(date);

  287. return LocalDate.of(localDate.getYear(), localDate.getMonth(), 1);

  288. }

  289.  
  290. /**

  291. * 特定日期的當月最後一天

  292. *

  293. * @param date

  294. * @return

  295. */

  296. public static LocalDate lastThisMonth(Date date) {

  297. int lastDay = getActualMaximum(date);

  298. LocalDate localDate = dateToLocalDate(date);

  299. return LocalDate.of(localDate.getYear(), localDate.getMonth(), lastDay);

  300. }

  301.  
  302.  
  303. /**

  304. * 特定日期的當年第一天

  305. *

  306. * @param date

  307. * @return

  308. */

  309. public static LocalDate newThisYear(Date date) {

  310. LocalDate localDate = dateToLocalDate(date);

  311. return LocalDate.of(localDate.getYear(), 1, 1);

  312.  
  313. }

  314.  
  315.  
  316. public static void main(String[] args) {

  317. }

  318.  
  319.  
  320. public static Timestamp getCurrentDateTime() {

  321. return new Timestamp(Instant.now().toEpochMilli());

  322.  
  323. }

  324.  
  325. /**

  326. * 獲取當前時間

  327. *

  328. * @return LocalDateTime

  329. */

  330. public static LocalDateTime getCurrentLocalDateTime() {

  331. return LocalDateTime.now(Clock.system(ZoneId.of("Asia/Shanghai")));

  332.  
  333. }

  334.  
  335.  
  336. /**

  337. * 修改日期時間的時間部分

  338. *

  339. * @param date

  340. * @param customTime 必須為"hh:mm:ss"這種格式

  341. */

  342. public static LocalDateTime reserveDateCustomTime(Date date, String customTime) {

  343. String dateStr = dateToLocalDate(date).toString() + " " + customTime;

  344. return stringToLocalDateTime(dateStr, "yyyy-MM-dd HH:mm:ss");

  345. }

  346.  
  347. /**

  348. * 修改日期時間的時間部分

  349. *

  350. * @param date

  351. * @param customTime 必須為"hh:mm:ss"這種格式

  352. */

  353. public static LocalDateTime reserveDateCustomTime(Timestamp date, String customTime) {

  354. String dateStr = timestampToLocalDate(date).toString() + " " + customTime;

  355. return stringToLocalDateTime(dateStr, "yyyy-MM-dd HH:mm:ss");

  356. }

  357.  
  358. /**

  359. * 把日期後的時間歸0 變成(yyyy-MM-dd 00:00:00:000)

  360. *

  361. * @param date

  362. * @return LocalDateTime

  363. */

  364. public static final LocalDateTime zerolizedTime(Date date) {

  365. LocalDateTime localDateTime = dateToLocalDateTime(date);

  366. return LocalDateTime.of(localDateTime.getYear(), localDateTime.getMonth(), localDateTime.getDayOfMonth(), 0, 0, 0, 0);

  367.  
  368. }

  369.  
  370. /**

  371. * 把時間變成(yyyy-MM-dd 00:00:00:000)

  372. *

  373. * @param date

  374. * @return LocalDateTime

  375. */

  376. public static LocalDateTime zerolizedTime(Timestamp date) {

  377. LocalDateTime localDateTime = timestampToLocalDateTime(date);

  378. return LocalDateTime.of(localDateTime.getYear(), localDateTime.getMonth(), localDateTime.getDayOfMonth(), 0, 0, 0, 0);

  379. }

  380.  
  381. /**

  382. * 把日期的時間變成(yyyy-MM-dd 23:59:59:999)

  383. *

  384. * @param date

  385. * @return LocalDateTime

  386. */

  387. public static LocalDateTime getEndTime(Date date) {

  388. LocalDateTime localDateTime = dateToLocalDateTime(date);

  389. return LocalDateTime.of(localDateTime.getYear(), localDateTime.getMonth(), localDateTime.getDayOfMonth(), 23, 59, 59, 999 * 1000000);

  390. }

  391.  
  392. /**

  393. * 把時間變成(yyyy-MM-dd 23:59:59:999)

  394. *

  395. * @param date

  396. * @return LocalDateTime

  397. */

  398. public static LocalDateTime getEndTime(Timestamp date) {

  399. LocalDateTime localDateTime = timestampToLocalDateTime(date);

  400. return LocalDateTime.of(localDateTime.getYear(), localDateTime.getMonth(), localDateTime.getDayOfMonth(), 23, 59, 59, 999 * 1000000);

  401. }

  402.  
  403. /**

  404. * 計算特定時間到 當天 23.59.59.999 的秒數

  405. *

  406. * @return

  407. */

  408. public static int calculateToEndTime(Date date) {

  409. LocalDateTime localDateTime = dateToLocalDateTime(date);

  410. LocalDateTime end = getEndTime(date);

  411. return (int) (end.toEpochSecond(ZoneOffset.UTC) - localDateTime.toEpochSecond(ZoneOffset.UTC));

  412. }

  413.  
  414.  
  415. /**

  416. * 增加或減少年/月/周/天/小時/分/秒數

  417. *

  418. * @param localDateTime 例:ChronoUnit.DAYS

  419. * @param chronoUnit

  420. * @param num

  421. * @return LocalDateTime

  422. */

  423. public static LocalDateTime addTime(LocalDateTime localDateTime, ChronoUnit chronoUnit, int num) {

  424. return localDateTime.plus(num, chronoUnit);

  425. }

  426.  
  427. /**

  428. * 增加或減少年/月/周/天/小時/分/秒數

  429. *

  430. * @param chronoUnit 例:ChronoUnit.DAYS

  431. * @param num

  432. * @return LocalDateTime

  433. */

  434. public static LocalDateTime addTime(Date date, ChronoUnit chronoUnit, int num) {

  435. long nanoOfSecond = (date.getTime() % 1000) * 1000000;

  436. LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(date.getTime() / 1000, (int) nanoOfSecond, ZoneOffset.of("+8"));

  437. return localDateTime.plus(num, chronoUnit);

  438. }

  439.  
  440. /**

  441. * 增加或減少年/月/周/天/小時/分/秒數

  442. *

  443. * @param chronoUnit 例:ChronoUnit.DAYS

  444. * @param num

  445. * @return LocalDateTime

  446. */

  447. public static LocalDateTime addTime(Timestamp date, ChronoUnit chronoUnit, int num) {

  448. long nanoOfSecond = (date.getTime() % 1000) * 1000000;

  449. LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(date.getTime() / 1000, (int) nanoOfSecond, ZoneOffset.of("+8"));

  450. return localDateTime.plus(num, chronoUnit);

  451. }

  452.  
  453. /**

  454. * Date 轉 LocalDateTime

  455. *

  456. * @param date

  457. * @return LocalDateTime

  458. */

  459. public static LocalDateTime dateToLocalDateTime(Date date) {

  460. long nanoOfSecond = (date.getTime() % 1000) * 1000000;

  461. LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(date.getTime() / 1000, (int) nanoOfSecond, ZoneOffset.of("+8"));

  462.  
  463. return localDateTime;

  464. }

  465.  
  466. /**

  467. * Timestamp 轉 LocalDateTime

  468. *

  469. * @param date

  470. * @return LocalDateTime

  471. */

  472. public static LocalDateTime timestampToLocalDateTime(Timestamp date) {

  473. LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(date.getTime() / 1000, date.getNanos(), ZoneOffset.of("+8"));

  474.  
  475. return localDateTime;

  476. }

  477.  
  478. /**

  479. * Date 轉 LocalDateTime

  480. *

  481. * @param date

  482. * @return LocalDate

  483. */

  484. public static LocalDate dateToLocalDate(Date date) {

  485.  
  486. return dateToLocalDateTime(date).toLocalDate();

  487. }

  488.  
  489. /**

  490. * timestamp 轉 LocalDateTime

  491. *

  492. * @param date

  493. * @return LocalDate

  494. */

  495. public static LocalDate timestampToLocalDate(Timestamp date) {

  496.  
  497. return timestampToLocalDateTime(date).toLocalDate();

  498. }

  499.  
  500. /**

  501. * 比較兩個LocalDateTime是否同一天

  502. *

  503. * @param begin

  504. * @param end

  505. * @return

  506. */

  507. public static boolean isTheSameDay(LocalDateTime begin, LocalDateTime end) {

  508. return begin.toLocalDate().equals(end.toLocalDate());

  509. }

  510.  
  511.  
  512. /**

  513. * 比較兩個時間LocalDateTime大小

  514. *

  515. * @param time1

  516. * @param time2

  517. * @return 1:第一個比第二個大;0:第一個與第二個相同;-1:第一個比第二個小

  518. */

  519. public static int compareTwoTime(LocalDateTime time1, LocalDateTime time2) {

  520.  
  521. if (time1.isAfter(time2)) {

  522. return 1;

  523. } else if (time1.isBefore(time2)) {

  524. return -1;

  525. } else {

  526. return 0;

  527. }

  528. }

  529.  
  530.  
  531. /**

  532. * 比較time1,time2兩個時間相差的秒數,如果time1<=time2,返回0

  533. *

  534. * @param time1

  535. * @param time2

  536. */

  537. public static long getTwoTimeDiffSecond(Timestamp time1, Timestamp time2) {

  538. long diff = timestampToLocalDateTime(time1).toEpochSecond(ZoneOffset.UTC) - timestampToLocalDateTime(time2).toEpochSecond(ZoneOffset.UTC);

  539. if (diff > 0) {

  540. return diff;

  541. } else {

  542. return 0;

  543. }

  544. }

  545.  
  546. /**

  547. * 比較time1,time2兩個時間相差的分鐘數,如果time1<=time2,返回0

  548. *

  549. * @param time1

  550. * @param time2

  551. */

  552. public static long getTwoTimeDiffMin(Timestamp time1, Timestamp time2) {

  553. long diff = getTwoTimeDiffSecond(time1, time2) / 60;

  554. if (diff > 0) {

  555. return diff;

  556. } else {

  557. return 0;

  558. }

  559. }

  560.  
  561. /**

  562. * 比較time1,time2兩個時間相差的小時數,如果time1<=time2,返回0

  563. *

  564. * @param time1

  565. * @param time2

  566. */

  567. public static long getTwoTimeDiffHour(Timestamp time1, Timestamp time2) {

  568. long diff = getTwoTimeDiffSecond(time1, time2) / 3600;

  569. if (diff > 0) {

  570. return diff;

  571. } else {

  572. return 0;

  573. }

  574. }

  575.  
  576. /**

  577. * 判斷當前時間是否在時間範圍內

  578. *

  579. * @param startTime

  580. * @param endTime

  581. * @return

  582. */

  583. public static boolean isTimeInRange(Date startTime, Date endTime) throws Exception {

  584. LocalDateTime now = getCurrentLocalDateTime();

  585. LocalDateTime start = dateToLocalDateTime(startTime);

  586. LocalDateTime end = dateToLocalDateTime(endTime);

  587. return (start.isBefore(now) && end.isAfter(now)) || start.isEqual(now) || end.isEqual(now);

  588. }

  589.  
  590. }