这个明天一定要完成这个,已经要将Date中的所有的功能都汇总起来
    1.直接根据年月日生成对应的时间
    2.能够解析时间
    3.反向解析时间
    4.时间向前移动一定的时间,和向后移动一定的时间
    5.借鉴apache中的所有的功能
    6.借鉴Heimdair中的DateUtil中的所有的功能

    1. import java.sql.Time;
    2. import java.sql.Timestamp;
    3. import java.text.ParseException;
    4. import java.text.SimpleDateFormat;
    5. import java.time.*;
    6. import java.time.format.DateTimeFormatter;
    7. import java.util.Date;
    8. import java.util.Map;
    9. import java.util.concurrent.ConcurrentHashMap;
    10. import java.util.regex.Pattern;
    11. /**
    12. * 时间格式的各种转换
    13. *
    14. * <ul>
    15. * <li>1.1.LocalDateTime {@code ---->} LocalDate</li>
    16. * <li>1.2.LocalDateTime {@code ---->} Long</li>
    17. * <li>1.3.LocalDateTime {@code ---->} Date</li>
    18. * <li>1.4.LocalDateTime {@code ---->} String</li>
    19. * <li>2.1.LocalDate {@code ---->} LocalDateTime</li>
    20. * <li>2.2.LocalDate {@code ---->} Long</li>
    21. * <li>2.3.LocalDate {@code ---->} Date</li>
    22. * <li>2.4.LocalDate {@code ---->} String</li>
    23. * <li>3.1.Date {@code ---->} LocalDateTime</li>
    24. * <li>3.2.Date {@code ---->} LocalDate</li>
    25. * <li>3.3.Date {@code ---->} Long</li>
    26. * <li>3.4.Date {@code ---->} String</li>
    27. * <li>4.1.Timestamp {@code ---->} LocalDateTime</li>
    28. * <li>4.2.Timestamp {@code ---->} Long</li>
    29. * <li>4.3.Timestamp {@code ---->} String</li>
    30. * <li>4.4.Timestamp {@code ---->} LocalDate</li>
    31. * <li>5.1.String {@code ---->} LocalDateTime</li>
    32. * <li>5.2.String {@code ---->} LocalDate</li>
    33. * <li>5.3.String {@code ---->} Date</li>
    34. * <li>5.4.String {@code ---->} Timestamp</li>
    35. * <li>5.5.String {@code ---->} LocalTime</li>
    36. * <li>5.6.String {@code ---->} Time</li>
    37. * <li>6.1.Long {@code ---->} Date</li>
    38. * <li>6.2.Long {@code ---->} LocalDateTime</li>
    39. * <li>6.3.Long {@code ---->} LocalDate</li>
    40. * <li>6.4.Long {@code ---->} String</li>
    41. * </ul>
    42. *
    43. * @author shizi
    44. * @since 2020/9/9 8:58 下午
    45. */
    46. public class LocalDateTimeUtil {
    47. public static final String yMdHmsSSS = "yyyy-MM-dd HH:mm:ss.SSS";
    48. public static final String yMdHmsS = "yyyy-MM-dd HH:mm:ss.S";
    49. public static final String yMdHms = "yyyy-MM-dd HH:mm:ss";
    50. public static final String yMdHm = "yyyy-MM-dd HH:mm";
    51. public static final String yMdH = "yyyy-MM-dd HH";
    52. public static final String yMd = "yyyy-MM-dd";
    53. public static final String yM = "yyyy-MM";
    54. public static final String y = "yyyy";
    55. public static final String yyyyMMdd = "yyyyMMdd";
    56. public static final String HmsSSSMore = "HH:mm:ss.SSSSSSSSS";
    57. public static final String HmsSSS = "HH:mm:ss.SSS";
    58. public static final String Hms = "HH:mm:ss";
    59. public static final String Hm = "HH:mm";
    60. public static final String H = "HH";
    61. private static final Pattern yMdHmsSSSPattern = Pattern.compile("^(\\d){4}-(\\d){2}-(\\d){2} (\\d){2}:(\\d){2}:(\\d){2}.(\\d){3}$");
    62. private static final Pattern yMdHmsSPattern = Pattern.compile("^(\\d){4}-(\\d){2}-(\\d){2} (\\d){2}:(\\d){2}:(\\d){2}.(\\d){1}$");
    63. private static final Pattern yMdHmsPattern = Pattern.compile("^(\\d){4}-(\\d){2}-(\\d){2} (\\d){2}:(\\d){2}:(\\d){2}$");
    64. private static final Pattern yMdHmPattern = Pattern.compile("^(\\d){4}-(\\d){2}-(\\d){2} (\\d){2}:(\\d){2}$");
    65. private static final Pattern yMdHPattern = Pattern.compile("^(\\d){4}-(\\d){2}-(\\d){2} (\\d){2}$");
    66. private static final Pattern yMdPattern = Pattern.compile("^(\\d){4}-(\\d){2}-(\\d){2}$");
    67. private static final Pattern yMPattern = Pattern.compile("^(\\d){4}-(\\d){2}$");
    68. private static final Pattern yPattern = Pattern.compile("^(\\d){4}$");
    69. private static final Map<String, SimpleDateFormat> simpleDateFormat = new ConcurrentHashMap<>(7);
    70. private static final Map<String, DateTimeFormatter> localDateTimeFormat = new ConcurrentHashMap<>(7);
    71. static {
    72. simpleDateFormat.put(yMdHmsSSS, new SimpleDateFormat(yMdHmsSSS));
    73. simpleDateFormat.put(yMdHms, new SimpleDateFormat(yMdHms));
    74. simpleDateFormat.put(yMdHm, new SimpleDateFormat(yMdHm));
    75. simpleDateFormat.put(yMdH, new SimpleDateFormat(yMdH));
    76. simpleDateFormat.put(yMd, new SimpleDateFormat(yMd));
    77. simpleDateFormat.put(yM, new SimpleDateFormat(yM));
    78. simpleDateFormat.put(y, new SimpleDateFormat(y));
    79. simpleDateFormat.put(yyyyMMdd, new SimpleDateFormat(yyyyMMdd));
    80. localDateTimeFormat.put(yMdHmsSSS, DateTimeFormatter.ofPattern(yMdHmsSSS));
    81. localDateTimeFormat.put(yMdHms, DateTimeFormatter.ofPattern(yMdHms));
    82. localDateTimeFormat.put(yMdHm, DateTimeFormatter.ofPattern(yMdHm));
    83. localDateTimeFormat.put(yMdH, DateTimeFormatter.ofPattern(yMdH));
    84. localDateTimeFormat.put(yMd, DateTimeFormatter.ofPattern(yMd));
    85. localDateTimeFormat.put(yM, DateTimeFormatter.ofPattern(yM));
    86. localDateTimeFormat.put(y, DateTimeFormatter.ofPattern(y));
    87. }
    88. /**
    89. * LocalDateTime 转 LocalDate
    90. *
    91. * @param localDateTime 待转换时间
    92. * @return 转换后的时间
    93. */
    94. private static LocalDate localDateTimeToLocalDate(LocalDateTime localDateTime) {
    95. if (null == localDateTime) {
    96. return null;
    97. }
    98. return localDateTime.toLocalDate();
    99. }
    100. /**
    101. * LocalDateTime 转 Long
    102. *
    103. * @param localDateTime 待转换时间
    104. * @return 转换后的时间
    105. */
    106. public static Long localDateTimeToLong(LocalDateTime localDateTime) {
    107. if (null == localDateTime) {
    108. return null;
    109. }
    110. return localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    111. }
    112. /**
    113. * LocalDateTime 转 Date
    114. *
    115. * @param localDateTime 待转换时间
    116. * @return 转换后的时间
    117. */
    118. public static Date localDateTimeToDate(LocalDateTime localDateTime) {
    119. if (null == localDateTime) {
    120. return null;
    121. }
    122. return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    123. }
    124. /**
    125. * LocalDateTime 转 String
    126. *
    127. * @param localDateTime 待转换时间
    128. * @return 转换后的时间
    129. */
    130. public static String localDateTimeToString(LocalDateTime localDateTime) {
    131. if (null == localDateTime) {
    132. return null;
    133. }
    134. return localDateTime.format(localDateTimeFormat.get(yMdHms));
    135. }
    136. /**
    137. * LocalDateTime 转 String
    138. *
    139. * @param localDateTime 待转换时间
    140. * @param dateTimeFormat 时间转换格式,建议使用{@link LocalDateTimeUtil#yMdHmsSSS}或者{@link LocalDateTimeUtil#yMdHms}等字段
    141. * @return 转换后的时间
    142. */
    143. public static String localDateTimeToString(LocalDateTime localDateTime, String dateTimeFormat) {
    144. if (null == localDateTime || null == dateTimeFormat) {
    145. return null;
    146. }
    147. return localDateTime.format(localDateTimeFormat.get(dateTimeFormat));
    148. }
    149. /**
    150. * LocalDateTime 转 String
    151. *
    152. * @param localDateTime 待转换时间
    153. * @param dateTimeFormatter 时间转换格式器
    154. * @return 转换后的时间
    155. */
    156. public static String localDateTimeToString(LocalDateTime localDateTime, DateTimeFormatter dateTimeFormatter) {
    157. if (null == localDateTime || null == dateTimeFormatter) {
    158. return null;
    159. }
    160. return localDateTime.format(dateTimeFormatter);
    161. }
    162. /**
    163. * LocalDate 转 LocalDateTime
    164. *
    165. * @param localDate 待转换时间
    166. * @return 转换后的时间
    167. */
    168. public static LocalDateTime localDateToLocalDateTime(LocalDate localDate) {
    169. if (null == localDate) {
    170. return null;
    171. }
    172. return LocalDateTime.of(localDate, LocalTime.parse("00:00:00"));
    173. }
    174. /**
    175. * LocalDate 转 Long
    176. *
    177. * @param localDate 待转换时间
    178. * @return 转换后的时间
    179. */
    180. public static Long localDateToLong(LocalDate localDate) {
    181. if (null == localDate) {
    182. return null;
    183. }
    184. return localDate.atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli();
    185. }
    186. /**
    187. * LocalDate 转 Date
    188. *
    189. * @param localDate 待转换时间
    190. * @return 转换后的时间
    191. */
    192. public static Date localDateToDate(LocalDate localDate) {
    193. if (null == localDate) {
    194. return null;
    195. }
    196. return Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
    197. }
    198. /**
    199. * LocalDate 转 String
    200. *
    201. * @param localDate 待转换时间
    202. * @return 转换后的时间
    203. */
    204. public static String localDateToString(LocalDate localDate) {
    205. if (null == localDate) {
    206. return null;
    207. }
    208. return localDate.format(localDateTimeFormat.get(yMd));
    209. }
    210. /**
    211. * LocalDate 转 String
    212. *
    213. * @param localTime 待转换时间
    214. * @return 转换后的时间
    215. */
    216. public static String localTimeToString(LocalTime localTime) {
    217. if (null == localTime) {
    218. return null;
    219. }
    220. return localTime.format(localDateTimeFormat.get(HmsSSS));
    221. }
    222. /**
    223. * Date 转 LocalDateTime
    224. *
    225. * @param date 待转换时间
    226. * @return 转换后的时间
    227. */
    228. public static LocalDateTime dateToLocalDateTime(Date date) {
    229. if (null == date) {
    230. return null;
    231. }
    232. return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
    233. }
    234. /**
    235. * Date 转 Long
    236. *
    237. * @param date 待转换时间
    238. * @return 转换后的时间
    239. */
    240. public static Long dateToLong(Date date) {
    241. if (null == date) {
    242. return null;
    243. }
    244. return date.getTime();
    245. }
    246. /**
    247. * Date 转 LocalDate
    248. *
    249. * @param date 待转换时间
    250. * @return 转换后的时间
    251. */
    252. public static LocalDate dateToLocalDate(Date date) {
    253. if (null == date) {
    254. return null;
    255. }
    256. return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).toLocalDate();
    257. }
    258. /**
    259. * Date 转 String
    260. *
    261. * @param date 待转换时间
    262. * @return 转换后的时间
    263. */
    264. public static String dateToString(Date date) {
    265. if (null == date) {
    266. return null;
    267. }
    268. return simpleDateFormat.get(yMdHms).format(date);
    269. }
    270. /**
    271. * Date 转 String
    272. *
    273. * @param date 待转换时间
    274. * @param simpleDateFormat 基本的格式转换器
    275. * @return 转换后的时间
    276. */
    277. public static String dateToString(Date date, SimpleDateFormat simpleDateFormat) {
    278. if (null == date || null == simpleDateFormat) {
    279. return null;
    280. }
    281. return simpleDateFormat.format(date);
    282. }
    283. /**
    284. * Date 转 String
    285. *
    286. * @param date 待转换时间
    287. * @param simpleDateFormatStr 时间转换格式,建议使用{@link LocalDateTimeUtil#yMdHmsSSS}或者{@link LocalDateTimeUtil#yMdHms}等字段
    288. * @return 转换后的时间
    289. */
    290. public static String dateToString(Date date, String simpleDateFormatStr) {
    291. if (null == date || null == simpleDateFormatStr) {
    292. return null;
    293. }
    294. return simpleDateFormat.get(simpleDateFormatStr).format(date);
    295. }
    296. /**
    297. * Timestamp 转 LocalDateTime
    298. *
    299. * @param timestamp 待转换时间
    300. * @return 转换后的时间
    301. */
    302. public static LocalDateTime timestampToLocalDateTime(Timestamp timestamp) {
    303. if (null == timestamp) {
    304. return null;
    305. }
    306. return timestamp.toLocalDateTime();
    307. }
    308. /**
    309. * Timestamp 转 Long
    310. *
    311. * @param timestamp 待转换时间
    312. * @return 转换后的时间
    313. */
    314. public static Long timestampToLong(Timestamp timestamp) {
    315. if (null == timestamp) {
    316. return null;
    317. }
    318. return timestamp.getTime();
    319. }
    320. /**
    321. * Timestamp 转 LocalDate
    322. *
    323. * @param timestamp 待转换时间
    324. * @return 转换后的时间
    325. */
    326. public static LocalDate timestampToLocalDate(Timestamp timestamp) {
    327. if (null == timestamp) {
    328. return null;
    329. }
    330. return LocalDateTime.ofInstant(timestamp.toInstant(), ZoneId.systemDefault()).toLocalDate();
    331. }
    332. /**
    333. * Timestamp 转 String
    334. *
    335. * @param timestamp 待转换时间
    336. * @return 转换后的时间
    337. */
    338. public static String timestampToString(Timestamp timestamp) {
    339. if (null == timestamp) {
    340. return null;
    341. }
    342. return simpleDateFormat.get(yMdHmsSSS).format(timestamp);
    343. }
    344. /**
    345. * Timestamp 转 String
    346. *
    347. * @param timestamp 待转换时间
    348. * @param simpleDateFormat 简单的时间转换格式
    349. * @return 转换后的时间
    350. */
    351. public static String timestampToString(Timestamp timestamp, SimpleDateFormat simpleDateFormat) {
    352. if (null == timestamp || null == simpleDateFormat) {
    353. return null;
    354. }
    355. return simpleDateFormat.format(timestamp);
    356. }
    357. /**
    358. * Timestamp 转 String
    359. *
    360. * @param timestamp 待转换时间
    361. * @param simpleDateFormatStr 时间转换格式,建议使用{@link LocalDateTimeUtil#yMdHmsSSS}或者{@link LocalDateTimeUtil#yMdHms}等字段
    362. * @return 转换后的时间
    363. */
    364. public static String timestampToString(Timestamp timestamp, String simpleDateFormatStr) {
    365. if (null == timestamp || null == simpleDateFormatStr) {
    366. return null;
    367. }
    368. return simpleDateFormat.get(simpleDateFormatStr).format(timestamp);
    369. }
    370. /**
    371. * String 转 LocalDateTime
    372. *
    373. * @param strDateTime 待转换时间
    374. * @return 转换后的时间
    375. */
    376. public static LocalDateTime stringToLocalDateTime(String strDateTime) {
    377. if (null == strDateTime) {
    378. return null;
    379. }
    380. return LocalDateTime.parse(strDateTime, DateTimeFormatter.ofPattern(getTimeFormat(strDateTime)));
    381. }
    382. /**
    383. * String 转 LocalDateTime
    384. *
    385. * @param strDateTime 待转换时间
    386. * @param formatStr 时间格式
    387. * @return 转换后的时间
    388. */
    389. public static LocalDateTime stringToLocalDateTime(String strDateTime, String formatStr) {
    390. if (null == strDateTime || null == formatStr) {
    391. return null;
    392. }
    393. return LocalDateTime.parse(strDateTime, DateTimeFormatter.ofPattern(formatStr));
    394. }
    395. /**
    396. * String 转 LocalDate
    397. *
    398. * @param strDateTime 待转换时间
    399. * @return 转换后的时间
    400. */
    401. public static LocalDate stringToLocalDate(String strDateTime) {
    402. if (null == strDateTime) {
    403. return null;
    404. }
    405. return LocalDate.parse(strDateTime, localDateTimeFormat.get(yMd));
    406. }
    407. /**
    408. * String 转 Date
    409. *
    410. * @param strDateTime 待转换时间
    411. * @return 转换后的时间
    412. */
    413. public static Date stringToDate(String strDateTime) {
    414. if (null == strDateTime) {
    415. return null;
    416. }
    417. try {
    418. return simpleDateFormat.get(getTimeFormat(strDateTime)).parse(strDateTime);
    419. } catch (ParseException e) {
    420. throw new RuntimeException(e);
    421. }
    422. }
    423. /**
    424. * String 转 Timestamp
    425. *
    426. * @param strDateTime 待转换时间
    427. * @return 转换后的时间
    428. */
    429. public static Timestamp stringToTimestamp(String strDateTime) {
    430. if (null == strDateTime) {
    431. return null;
    432. }
    433. return Timestamp.valueOf(strDateTime);
    434. }
    435. /**
    436. * String 转 LocalTime
    437. *
    438. * @param strDateTime 待转换时间
    439. * @param datetimeFormat 时间格式
    440. * @return 转换后的时间
    441. */
    442. public static LocalTime stringToLocalTime(String strDateTime, String datetimeFormat) {
    443. if (null == strDateTime || null == datetimeFormat) {
    444. return null;
    445. }
    446. return LocalTime.parse(strDateTime, DateTimeFormatter.ofPattern(datetimeFormat));
    447. }
    448. /**
    449. * String 转 LocalTime
    450. *
    451. * @param strDateTime 待转换时间
    452. * @return 转换后的时间
    453. */
    454. public static LocalTime stringToLocalTime(String strDateTime) {
    455. if (null == strDateTime) {
    456. return null;
    457. }
    458. return LocalTime.parse(strDateTime, DateTimeFormatter.ofPattern(HmsSSS));
    459. }
    460. /**
    461. * String 转 Time
    462. *
    463. * @param strDateTime 待转换时间
    464. * @return 转换后的时间
    465. */
    466. public static Time stringToTime(String strDateTime) {
    467. if (null == strDateTime) {
    468. return null;
    469. }
    470. return Time.valueOf(strDateTime);
    471. }
    472. /**
    473. * Long 转 Date
    474. *
    475. * @param time 待转换时间
    476. * @return 转换后的时间
    477. */
    478. public static Date longToDate(Long time) {
    479. if (null == time) {
    480. return null;
    481. }
    482. return new Date(time);
    483. }
    484. /**
    485. * Long 转 LocalDateTime
    486. *
    487. * @param time 待转换时间
    488. * @return 转换后的时间
    489. */
    490. public static LocalDateTime longToLocalDateTime(Long time) {
    491. if (null == time) {
    492. return null;
    493. }
    494. return LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault());
    495. }
    496. /**
    497. * Long 转 LocalDate
    498. *
    499. * @param time 待转换时间
    500. * @return 转换后的时间
    501. */
    502. public static LocalDate longToLocalDate(Long time) {
    503. if (null == time) {
    504. return null;
    505. }
    506. return LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault()).toLocalDate();
    507. }
    508. /**
    509. * Long 转 String
    510. *
    511. * @param time 待转换时间
    512. * @return 转换后的时间
    513. */
    514. public static String longToString(Long time) {
    515. if (null == time) {
    516. return null;
    517. }
    518. return simpleDateFormat.get(yMdHms).format(new Date(time));
    519. }
    520. /**
    521. * Long 转 String
    522. *
    523. * @param time 待转换时间
    524. * @param formatKey 时间转换格式,建议使用{@link LocalDateTimeUtil#yMdHmsSSS}或者{@link LocalDateTimeUtil#yMdHms}等字段
    525. * @return 转换后的时间
    526. */
    527. public static String longToString(Long time, String formatKey) {
    528. if (null == time || null == formatKey) {
    529. return null;
    530. }
    531. return simpleDateFormat.get(formatKey).format(new Date(time));
    532. }
    533. /**
    534. * Long 转 java.sql.Date
    535. *
    536. * @param time long类型的时间
    537. * @return 日期
    538. */
    539. public static java.sql.Date longToSqlDate(Long time) {
    540. if (null == time) {
    541. return null;
    542. }
    543. return new java.sql.Date(time);
    544. }
    545. /**
    546. * Long 转 java.sql.Time
    547. *
    548. * @param time long类型的时间
    549. * @return 日期
    550. */
    551. public static java.sql.Time longToTime(Long time) {
    552. if (null == time) {
    553. return null;
    554. }
    555. return new java.sql.Time(time);
    556. }
    557. /**
    558. * Long 转 java.sql.Timestamp
    559. *
    560. * @param time long类型的时间
    561. * @return 日期
    562. */
    563. public static Timestamp longToTimestamp(Long time) {
    564. if (null == time) {
    565. return null;
    566. }
    567. return new java.sql.Timestamp(time);
    568. }
    569. public static Date setTime(Date date, Integer year, Integer month, Integer day, Integer hour, Integer minute, Integer second) {
    570. if (null == date) {
    571. return null;
    572. }
    573. Integer yearCopy = year;
    574. if (null == year) {
    575. yearCopy = 0;
    576. }
    577. Integer monthCopy = month;
    578. if (null == month) {
    579. monthCopy = 0;
    580. }
    581. Integer dayCopy = day;
    582. if (null == day) {
    583. dayCopy = 0;
    584. }
    585. Integer hourCopy = hour;
    586. if (null == hour) {
    587. hourCopy = 0;
    588. }
    589. Integer minuteCopy = minute;
    590. if (null == minute) {
    591. minuteCopy = 0;
    592. }
    593. Integer secondCopy = second;
    594. if (null == second) {
    595. secondCopy = 0;
    596. }
    597. return localDateTimeToDate(
    598. dateToLocalDateTime(date).withYear(yearCopy).withMonth(monthCopy).withDayOfMonth(dayCopy).withHour(hourCopy).withMinute(minuteCopy).withSecond(secondCopy));
    599. }
    600. public static Date setYear(Date date, Integer year) {
    601. if (null == date) {
    602. return null;
    603. }
    604. Integer yearCopy = year;
    605. if (null == year) {
    606. yearCopy = 0;
    607. }
    608. return localDateTimeToDate(dateToLocalDateTime(date).withYear(yearCopy));
    609. }
    610. public static Date setMonth(Date date, Integer month) {
    611. if (null == date) {
    612. return null;
    613. }
    614. Integer monthCopy = month;
    615. if (null == month) {
    616. monthCopy = 0;
    617. }
    618. return localDateTimeToDate(dateToLocalDateTime(date).withMonth(monthCopy));
    619. }
    620. public static Date setDay(Date date, Integer day) {
    621. if (null == date) {
    622. return null;
    623. }
    624. Integer dayCopy = day;
    625. if (null == day) {
    626. dayCopy = 0;
    627. }
    628. return localDateTimeToDate(dateToLocalDateTime(date).withDayOfMonth(dayCopy));
    629. }
    630. public static Date setHour(Date date, Integer hour) {
    631. if (null == date) {
    632. return null;
    633. }
    634. Integer hourCopy = hour;
    635. if (null == hour) {
    636. hourCopy = 0;
    637. }
    638. return localDateTimeToDate(dateToLocalDateTime(date).withHour(hourCopy));
    639. }
    640. public static Date setMinute(Date date, Integer minute) {
    641. if (null == date) {
    642. return null;
    643. }
    644. Integer minuteCopy = minute;
    645. if (null == minute) {
    646. minuteCopy = 0;
    647. }
    648. return localDateTimeToDate(dateToLocalDateTime(date).withMinute(minuteCopy));
    649. }
    650. public static Date setSecond(Date date, Integer second) {
    651. if (null == date) {
    652. return null;
    653. }
    654. Integer secondCopy = second;
    655. if (null == second) {
    656. secondCopy = 0;
    657. }
    658. return localDateTimeToDate(dateToLocalDateTime(date).withSecond(secondCopy));
    659. }
    660. public static Long setTime(Long time, Integer year, Integer month, Integer day, Integer hour, Integer minute, Integer second) {
    661. if (null == time || null == year || null == month || null == day || null == hour || null == minute || null == second) {
    662. return null;
    663. }
    664. return localDateTimeToLong(longToLocalDateTime(time).withYear(year).withMonth(month).withDayOfMonth(day).withHour(hour).withMinute(minute).withSecond(second));
    665. }
    666. public static Long setYear(Long time, Integer year) {
    667. if (null == time || null == year) {
    668. return null;
    669. }
    670. return localDateTimeToLong(longToLocalDateTime(time).withYear(year));
    671. }
    672. public static Long setMonth(Long time, Integer month) {
    673. if (null == time || null == month) {
    674. return null;
    675. }
    676. return localDateTimeToLong(longToLocalDateTime(time).withMonth(month));
    677. }
    678. public static Long setDay(Long time, Integer day) {
    679. if (null == time || null == day) {
    680. return null;
    681. }
    682. return localDateTimeToLong(longToLocalDateTime(time).withDayOfMonth(day));
    683. }
    684. public static Long setHour(Long time, Integer hour) {
    685. if (null == time || null == hour) {
    686. return null;
    687. }
    688. return localDateTimeToLong(longToLocalDateTime(time).withHour(hour));
    689. }
    690. public static Long setMinute(Long time, Integer minute) {
    691. if (null == time || null == minute) {
    692. return null;
    693. }
    694. return localDateTimeToLong(longToLocalDateTime(time).withMinute(minute));
    695. }
    696. public static Long setSecond(Long time, Integer second) {
    697. if (null == time || null == second) {
    698. return null;
    699. }
    700. return localDateTimeToLong(longToLocalDateTime(time).withSecond(second));
    701. }
    702. public static Date plusTime(Date date, Integer years, Integer months, Integer days, Integer hours, Integer minutes, Integer seconds) {
    703. if (null == date) {
    704. return null;
    705. }
    706. Integer yearsCopy = years;
    707. if (null == years) {
    708. yearsCopy = 0;
    709. }
    710. Integer monthsCopy = months;
    711. if (null == months) {
    712. monthsCopy = 0;
    713. }
    714. Integer daysCopy = days;
    715. if (null == days) {
    716. daysCopy = 0;
    717. }
    718. Integer hoursCopy = hours;
    719. if (null == hours) {
    720. hoursCopy = 0;
    721. }
    722. Integer minutesCopy = minutes;
    723. if (null == minutes) {
    724. minutesCopy = 0;
    725. }
    726. Integer secondsCopy = seconds;
    727. if (null == seconds) {
    728. secondsCopy = 0;
    729. }
    730. return localDateTimeToDate(
    731. dateToLocalDateTime(date).plusYears(yearsCopy).plusMonths(monthsCopy).plusDays(daysCopy).plusHours(hoursCopy).plusMinutes(minutesCopy).plusSeconds(secondsCopy));
    732. }
    733. public static Date plusYears(Date date, Integer years) {
    734. if (null == date) {
    735. return null;
    736. }
    737. Integer yearsCopy = years;
    738. if (null == years) {
    739. yearsCopy = 0;
    740. }
    741. return localDateTimeToDate(dateToLocalDateTime(date).plusYears(yearsCopy));
    742. }
    743. public static Date plusMonths(Date date, Integer months) {
    744. if (null == date) {
    745. return null;
    746. }
    747. Integer monthsCopy = months;
    748. if (null == months) {
    749. monthsCopy = 0;
    750. }
    751. return localDateTimeToDate(dateToLocalDateTime(date).plusMonths(monthsCopy));
    752. }
    753. public static Date plusDays(Date date, Integer days) {
    754. if (null == date) {
    755. return null;
    756. }
    757. Integer daysCopy = days;
    758. if (null == days) {
    759. daysCopy = 0;
    760. }
    761. return localDateTimeToDate(dateToLocalDateTime(date).plusDays(daysCopy));
    762. }
    763. public static Date plusHours(Date date, Integer hours) {
    764. if (null == date) {
    765. return null;
    766. }
    767. Integer hoursCopy = hours;
    768. if (null == hours) {
    769. hoursCopy = 0;
    770. }
    771. return localDateTimeToDate(dateToLocalDateTime(date).plusHours(hoursCopy));
    772. }
    773. public static Date plusMinutes(Date date, Integer minutes) {
    774. if (null == date) {
    775. return null;
    776. }
    777. Integer minutesCopy = minutes;
    778. if (null == minutes) {
    779. minutesCopy = 0;
    780. }
    781. return localDateTimeToDate(dateToLocalDateTime(date).plusMinutes(minutesCopy));
    782. }
    783. public static Date plusSeconds(Date date, Integer seconds) {
    784. if (null == date) {
    785. return null;
    786. }
    787. Integer secondsCopy = seconds;
    788. if (null == seconds) {
    789. secondsCopy = 0;
    790. }
    791. return localDateTimeToDate(dateToLocalDateTime(date).plusSeconds(secondsCopy));
    792. }
    793. public static Long plusTime(Long time, Integer years, Integer months, Integer days, Integer hours, Integer minutes, Integer seconds) {
    794. if (null == time) {
    795. return null;
    796. }
    797. Integer yearsCopy = years;
    798. if (null == years) {
    799. yearsCopy = 0;
    800. }
    801. Integer monthsCopy = months;
    802. if (null == months) {
    803. monthsCopy = 0;
    804. }
    805. Integer daysCopy = days;
    806. if (null == days) {
    807. daysCopy = 0;
    808. }
    809. Integer hoursCopy = hours;
    810. if (null == hours) {
    811. hoursCopy = 0;
    812. }
    813. Integer minutesCopy = minutes;
    814. if (null == minutes) {
    815. minutesCopy = 0;
    816. }
    817. Integer secondsCopy = seconds;
    818. if (null == seconds) {
    819. secondsCopy = 0;
    820. }
    821. return localDateTimeToLong(
    822. longToLocalDateTime(time).plusYears(yearsCopy).plusMonths(monthsCopy).plusDays(daysCopy).plusHours(hoursCopy).plusMinutes(minutesCopy).plusSeconds(secondsCopy));
    823. }
    824. public static Long plusYears(Long time, Integer years) {
    825. if (null == time) {
    826. return null;
    827. }
    828. Integer yearsCopy = years;
    829. if (null == years) {
    830. yearsCopy = 0;
    831. }
    832. return localDateTimeToLong(longToLocalDateTime(time).plusYears(yearsCopy));
    833. }
    834. public static Long plusMonths(Long time, Integer months) {
    835. if (null == time) {
    836. return null;
    837. }
    838. Integer monthsCopy = months;
    839. if (null == months) {
    840. monthsCopy = 0;
    841. }
    842. return localDateTimeToLong(longToLocalDateTime(time).plusMonths(monthsCopy));
    843. }
    844. public static Long plusDays(Long time, Integer days) {
    845. if (null == time) {
    846. return null;
    847. }
    848. Integer daysCopy = days;
    849. if (null == days) {
    850. daysCopy = 0;
    851. }
    852. return localDateTimeToLong(longToLocalDateTime(time).plusDays(daysCopy));
    853. }
    854. public static Long plusHours(Long time, Integer hours) {
    855. if (null == time) {
    856. return null;
    857. }
    858. Integer hoursCopy = hours;
    859. if (null == hours) {
    860. hoursCopy = 0;
    861. }
    862. return localDateTimeToLong(longToLocalDateTime(time).plusHours(hoursCopy));
    863. }
    864. public static Long plusMinutes(Long time, Integer minutes) {
    865. if (null == time) {
    866. return null;
    867. }
    868. Integer minutesCopy = minutes;
    869. if (null == minutes) {
    870. minutesCopy = 0;
    871. }
    872. return localDateTimeToLong(longToLocalDateTime(time).plusMinutes(minutesCopy));
    873. }
    874. public static Long plusSeconds(Long time, Integer seconds) {
    875. if (null == time) {
    876. return null;
    877. }
    878. Integer secondsCopy = seconds;
    879. if (null == seconds) {
    880. secondsCopy = 0;
    881. }
    882. return localDateTimeToLong(longToLocalDateTime(time).plusSeconds(secondsCopy));
    883. }
    884. public static Date setDayStart(Date date) {
    885. if (null == date) {
    886. return null;
    887. }
    888. return localDateTimeToDate(dateToLocalDateTime(date).withHour(0).withMinute(0).withSecond(0));
    889. }
    890. public static Long setDayStart(Long time) {
    891. if (null == time) {
    892. return null;
    893. }
    894. return localDateTimeToLong(longToLocalDateTime(time).withHour(0).withMinute(0).withSecond(0).withNano(0));
    895. }
    896. public static Date setDayEnd(Date date) {
    897. if (null == date) {
    898. return null;
    899. }
    900. return localDateTimeToDate(dateToLocalDateTime(date).withHour(23).withMinute(59).withSecond(59));
    901. }
    902. public static Long setDayEnd(Long time) {
    903. if (null == time) {
    904. return null;
    905. }
    906. return localDateTimeToLong(longToLocalDateTime(time).withHour(23).withMinute(59).withSecond(59));
    907. }
    908. private static String getTimeFormat(String strDateTime) {
    909. if (null == strDateTime) {
    910. throw new RuntimeException("获取时间格式错误, time =" + strDateTime);
    911. }
    912. String data;
    913. data = strDateTime.trim();
    914. if ("".equals(data) || "null".equals(data)) {
    915. throw new RuntimeException("获取时间格式错误, time =" + strDateTime);
    916. }
    917. String timeFormat = yMdHms;
    918. if (yPattern.matcher(data).matches()) {
    919. timeFormat = y;
    920. } else if (yMPattern.matcher(data).matches()) {
    921. timeFormat = yM;
    922. } else if (yMdPattern.matcher(data).matches()) {
    923. timeFormat = yMd;
    924. } else if (yMdHPattern.matcher(data).matches()) {
    925. timeFormat = yMdH;
    926. } else if (yMdHmPattern.matcher(data).matches()) {
    927. timeFormat = yMdHm;
    928. } else if (yMdHmsPattern.matcher(data).matches()) {
    929. timeFormat = yMdHms;
    930. } else if (yMdHmsSPattern.matcher(data).matches()) {
    931. timeFormat = yMdHmsS;
    932. } else if (yMdHmsSSSPattern.matcher(data).matches()) {
    933. timeFormat = yMdHmsSSS;
    934. }
    935. return timeFormat;
    936. }
    937. }