日期时间工具类

冷不防 2024-03-17 12:06 210阅读 0赞

直接调用下方法即可获取当前时间:

  1. new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());

使用工具类获取当前时间(需引入下面的DateUtils工具类):

  1. DateUtils.formatDateTime(new Date());
  1. package com.zyq.util.date;
  2. import java.text.ParseException;
  3. import java.text.SimpleDateFormat;
  4. import java.util.ArrayList;
  5. import java.util.Calendar;
  6. import java.util.Collections;
  7. import java.util.Date;
  8. import java.util.HashMap;
  9. import java.util.List;
  10. import java.util.Map;
  11. import java.util.Objects;
  12. /**
  13. * 创作者链接:https://blog.csdn.net/sunnyzyq/article/details/125377621
  14. * @author zyq
  15. * @since 2022/6/17
  16. */
  17. public class DateUtils {
  18. /** 一星期的天数 */
  19. public static final int WEEK_DAYS = 7;
  20. /** 一年的月份数 */
  21. public static final int YEAR_MONTHS = 12;
  22. /** 一天的小时数 */
  23. public static final int DAY_HOURS = 24;
  24. /** 一小时分钟数 */
  25. public static final int HOUR_MINUTES = 60;
  26. /** 一天分钟数 (24 * 60) */
  27. public static final int DAY_MINUTES = 1440;
  28. /** 一分钟的秒数 */
  29. public static final int MINUTE_SECONDS = 60;
  30. /** 一个小时的秒数 (60 * 60) */
  31. public static final int HOUR_SECONDS = 3600;
  32. /** 一天的秒数 (24 * 60 * 60) */
  33. public static final int DAY_SECONDS = 86400;
  34. /** 一秒的毫秒数 */
  35. public static final long SECOND_MILLISECONDS = 1000L;
  36. /** 一分钟的毫秒数(60 * 1000) */
  37. public static final long MINUTE_MILLISECONDS = 60000L;
  38. /** 一小时的毫秒数(60 * 60 * 1000) */
  39. public static final long HOUR_MILLISECONDS = 3600000L;
  40. /** 一天的毫秒数(24 * 60* 60* 1000) */
  41. public static final long DAY_MILLISECONDS = 86400000L;
  42. /** 星期一 */
  43. public static final int WEEK_1_MONDAY = 1;
  44. /** 星期二 */
  45. public static final int WEEK_2_TUESDAY = 2;
  46. /** 星期三 */
  47. public static final int WEEK_3_WEDNESDAY = 3;
  48. /** 星期四 */
  49. public static final int WEEK_4_THURSDAY = 4;
  50. /** 星期五 */
  51. public static final int WEEK_5_FRIDAY = 5;
  52. /** 星期六 */
  53. public static final int WEEK_6_SATURDAY = 6;
  54. /** 星期天 */
  55. public static final int WEEK_7_SUNDAY = 7;
  56. /** 一月 */
  57. public static final int MONTH_1_JANUARY = 1;
  58. /** 二月 */
  59. public static final int MONTH_2_FEBRUARY = 2;
  60. /** 三月 */
  61. public static final int MONTH_3_MARCH = 3;
  62. /** 四月 */
  63. public static final int MONTH_4_APRIL= 4;
  64. /** 五月 */
  65. public static final int MONTH_5_MAY = 5;
  66. /** 六月 */
  67. public static final int MONTH_6_JUNE = 6;
  68. /** 七月 */
  69. public static final int MONTH_7_JULY = 7;
  70. /** 八月 */
  71. public static final int MONTH_8_AUGUST = 8;
  72. /** 九月 */
  73. public static final int MONTH_9_SEPTEMBER = 9;
  74. /** 十月 */
  75. public static final int MONTH_10_OCTOBER = 10;
  76. /** 十一月 */
  77. public static final int MONTH_11_NOVEMBER = 11;
  78. /** 十二月 */
  79. public static final int MONTH_12_DECEMBER= 12;
  80. /** 显示到日期 */
  81. public static final String FORMAT_DATE = "yyyy-MM-dd";
  82. /** 显示到小时 */
  83. public static final String FORMAT_HOUR = "yyyy-MM-dd HH";
  84. /** 显示到分 */
  85. public static final String FORMAT_MINUTE = "yyyy-MM-dd HH:mm";
  86. /** 显示到秒 */
  87. public static final String FORMAT_SECOND = "yyyy-MM-dd HH:mm:ss";
  88. /** 显示到毫秒 */
  89. public static final String FORMAT_MILLISECOND = "yyyy-MM-dd HH:mm:ss:SSS";
  90. /** 显示到日期(数字格式) */
  91. public static final String FORMAT_NO_DATE = "yyyyMMdd";
  92. /** 显示到小时(数字格式) */
  93. public static final String FORMAT_NO_HOUR = "yyyyMMddHH";
  94. /** 显示到分(数字格式) */
  95. public static final String FORMAT_NO_MINUTE = "yyyyMMddHHmm";
  96. /** 显示到秒(数字格式) */
  97. public static final String FORMAT_NO_SECOND = "yyyyMMddHHmmss";
  98. /** 显示到毫秒(数字格式) */
  99. public static final String FORMAT_NO_MILLISECOND = "yyyyMMddHHmmssSSS";
  100. /**
  101. * 获取指定时间格式化器
  102. *
  103. * @param formatStyle 时间格式
  104. * @return 时间格式化器
  105. */
  106. private static SimpleDateFormat getSimpleDateFormat(String formatStyle) {
  107. return new SimpleDateFormat(formatStyle);
  108. }
  109. /**
  110. * 将 Date 格式时间转化为指定格式时间
  111. *
  112. * @param date Date 格式时间
  113. * @param formatStyle 转化指定格式(如: yyyy-MM-dd HH:mm:ss)
  114. * @return 转化格式时间
  115. */
  116. public static String format(Date date, String formatStyle) {
  117. if (Objects.isNull(date)) {
  118. return "";
  119. }
  120. return getSimpleDateFormat(formatStyle).format(date);
  121. }
  122. /**
  123. * 将 Date 格式时间转化为 yyyy-MM-dd 格式时间
  124. *
  125. * @param date Date 格式时间
  126. * @return yyyy-MM-dd 格式时间(如:2022-06-17)
  127. */
  128. public static String formatDate(Date date) {
  129. return format(date, FORMAT_DATE);
  130. }
  131. /**
  132. * 将 Date 格式时间转化为 yyyy-MM-dd HH:mm:ss 格式时间
  133. *
  134. * @param date Date 格式时间
  135. * @return yyyy-MM-dd HH:mm:ss 格式时间(如:2022-06-17 16:06:17)
  136. */
  137. public static String formatDateTime(Date date) {
  138. return format(date, FORMAT_SECOND);
  139. }
  140. /**
  141. * 将 Date 格式时间转化为 yyyy-MM-dd HH:mm:ss:SSS 格式时间
  142. *
  143. * @param date Date 格式时间
  144. * @return yyyy-MM-dd HH:mm:ss:SSS 格式时间(如:2022-06-17 16:06:17:325)
  145. */
  146. public static String formatDateTimeStamp(Date date) {
  147. return format(date, FORMAT_MILLISECOND);
  148. }
  149. /**
  150. * 将 yyyy-MM-dd 格式时间转化为 Date 格式时间
  151. *
  152. * @param dateString yyyy-MM-dd 格式时间(如:2022-06-17)
  153. * @return Date 格式时间
  154. */
  155. public static Date parseDate(String dateString) {
  156. return parse(dateString, FORMAT_DATE);
  157. }
  158. /**
  159. * 将 yyyy-MM-dd HH:mm:ss 格式时间转化为 Date 格式时间
  160. *
  161. * @param dateTimeStr yyyy-MM-dd HH:mm:ss 格式时间(如:2022-06-17 16:06:17)
  162. * @return Date 格式时间
  163. */
  164. public static Date parseDateTime(String dateTimeStr) {
  165. return parse(dateTimeStr, FORMAT_SECOND);
  166. }
  167. /**
  168. * 将 yyyy-MM-dd HH:mm:ss:SSS 格式时间转化为 Date 格式时间
  169. *
  170. * @param dateTimeStr yyyy-MM-dd HH:mm:ss:SSS 格式时间(如:2022-06-17 16:06:17)
  171. * @return Date 格式时间
  172. */
  173. public static Date parseDateTimeStamp(String dateTimeStampStr) {
  174. return parse(dateTimeStampStr, FORMAT_MILLISECOND);
  175. }
  176. /**
  177. * 将字符串格式时间转化为 Date 格式时间
  178. *
  179. * @param dateString 字符串时间(如:2022-06-17 16:06:17)
  180. * @return formatStyle 格式内容
  181. * @return Date 格式时间
  182. */
  183. public static Date parse(String dateString, String formatStyle) {
  184. String s = getString(dateString);
  185. if (s.isEmpty()) {
  186. return null;
  187. }
  188. try {
  189. return getSimpleDateFormat(formatStyle).parse(dateString);
  190. } catch (ParseException e) {
  191. e.printStackTrace();
  192. return null;
  193. }
  194. }
  195. /**
  196. * 获取字符串有效内容
  197. *
  198. * @param s 字符串
  199. * @return 有效内容
  200. */
  201. private static String getString(String s) {
  202. return Objects.isNull(s) ? "" : s.trim();
  203. }
  204. /**
  205. * 获取一天的开始时间(即:0 点 0 分 0 秒 0 毫秒)
  206. *
  207. * @param date 指定时间
  208. * @return 当天的开始时间
  209. */
  210. public static Date getDateStart(Date date) {
  211. if (Objects.isNull(date)) {
  212. return null;
  213. }
  214. Calendar calendar = Calendar.getInstance();
  215. calendar.setTime(date);
  216. calendar.set(Calendar.HOUR_OF_DAY, 0);
  217. calendar.set(Calendar.MINUTE, 0);
  218. calendar.set(Calendar.SECOND, 0);
  219. calendar.set(Calendar.MILLISECOND, 0);
  220. return calendar.getTime();
  221. }
  222. /**
  223. * 获取一天的截止时间(即:23 点 59 分 59 秒 999 毫秒)
  224. *
  225. * @param date 指定时间
  226. * @return 当天的开始时间
  227. */
  228. public static Date getDateEnd(Date date) {
  229. if (Objects.isNull(date)) {
  230. return null;
  231. }
  232. Calendar calendar = Calendar.getInstance();
  233. calendar.setTime(date);
  234. calendar.set(Calendar.HOUR_OF_DAY, 23);
  235. calendar.set(Calendar.MINUTE, 59);
  236. calendar.set(Calendar.SECOND, 59);
  237. calendar.set(Calendar.MILLISECOND, 999);
  238. return calendar.getTime();
  239. }
  240. /**
  241. * 获取日期数字
  242. *
  243. * @param date 日期
  244. * @return 日期数字
  245. */
  246. public static int getDateNo(Date date) {
  247. if (Objects.isNull(date)) {
  248. return 0;
  249. }
  250. return Integer.valueOf(format(date, FORMAT_NO_DATE));
  251. }
  252. /**
  253. * 获取日期时间数字(到秒)
  254. *
  255. * @param date 日期
  256. * @return 日期数字
  257. */
  258. public static long getDateTimeNo(Date date) {
  259. if (Objects.isNull(date)) {
  260. return 0L;
  261. }
  262. return Long.valueOf(format(date, FORMAT_NO_SECOND));
  263. }
  264. /**
  265. * 获取日期时间数字(到毫秒)
  266. *
  267. * @param date 日期
  268. * @return 日期数字
  269. */
  270. public static long getDateTimeStampNo(Date date) {
  271. if (Objects.isNull(date)) {
  272. return 0L;
  273. }
  274. return Long.valueOf(format(date, FORMAT_NO_MILLISECOND));
  275. }
  276. /**
  277. * 获取星期几
  278. *
  279. * @param date 时间
  280. * @return 0(时间为空), 1(周一), 2(周二),3(周三),4(周四),5(周五),6(周六),7(周日)
  281. */
  282. public static int getWeek(Date date) {
  283. if (Objects.isNull(date)) {
  284. return 0;
  285. }
  286. Calendar calendar = Calendar.getInstance();
  287. calendar.setTime(date);
  288. return getWeek(calendar);
  289. }
  290. /**
  291. * 获取星期几
  292. *
  293. * @param date 时间
  294. * @return 0(时间为空), 1(周一), 2(周二),3(周三),4(周四),5(周五),6(周六),7(周日)
  295. */
  296. private static int getWeek(Calendar calendar) {
  297. switch (calendar.get(Calendar.DAY_OF_WEEK)) {
  298. case Calendar.MONDAY:
  299. return 1;
  300. case Calendar.TUESDAY:
  301. return 2;
  302. case Calendar.WEDNESDAY:
  303. return 3;
  304. case Calendar.THURSDAY:
  305. return 4;
  306. case Calendar.FRIDAY:
  307. return 5;
  308. case Calendar.SATURDAY:
  309. return 6;
  310. case Calendar.SUNDAY:
  311. return 7;
  312. default:
  313. return 0;
  314. }
  315. }
  316. /**
  317. * 获取该日期是今年的第几周(以本年的周一为第1周,详见下面说明)<br>
  318. *
  319. * 【说明】<br>
  320. * 比如 2022-01-01(周六)和 2022-01-02(周日)虽然在 2022 年里,但他们两天则属于 2021 年最后一周,<br>
  321. * 那么这两天不会算在 2022 年第 1 周里,此时会返回 0 ;而 2022 年第 1 周将从 2022-01-03(周一) 开始计算。<br>
  322. *
  323. * @param date 时间
  324. * @return -1(时间为空), 0(为上个年的最后一周),其他数字(今年的第几周)
  325. */
  326. public static int getWeekOfYear(Date date) {
  327. if (Objects.isNull(date)) {
  328. return -1;
  329. }
  330. int weeks = getWeekOfYearIgnoreLastYear(date);
  331. Calendar calendar = Calendar.getInstance();
  332. calendar.setTime(date);
  333. calendar.set(Calendar.MONTH, Calendar.JANUARY);
  334. calendar.set(Calendar.DAY_OF_MONTH, 1);
  335. int week = getWeek(calendar);
  336. if (week == 1) {
  337. return weeks;
  338. }
  339. return weeks - 1;
  340. }
  341. /**
  342. * 获取今年的第几周(以本年的1月1日为第1周第1天)<br>
  343. *
  344. * @param date 时间
  345. * @return -1(时间为空),其他数字(今年的第几周)
  346. */
  347. public static int getWeekOfYearIgnoreLastYear(Date date) {
  348. if (Objects.isNull(date)) {
  349. return -1;
  350. }
  351. Calendar calendar = Calendar.getInstance();
  352. calendar.setTime(date);
  353. int days = calendar.get(Calendar.DAY_OF_YEAR);
  354. int weeks = days / 7;
  355. // 如果是 7 的倍数,则表示恰好是多少周
  356. if (days % 7 == 0) {
  357. return weeks;
  358. }
  359. // 如果有余数,则需要再加 1
  360. return weeks + 1;
  361. }
  362. /**
  363. * 获取时间节点对象
  364. *
  365. * @param date 时间对象
  366. * @return DateNode
  367. */
  368. public static DateNode getDateNode(Date date) {
  369. if (Objects.isNull(date)) {
  370. return null;
  371. }
  372. Calendar calendar = Calendar.getInstance();
  373. calendar.setTime(date);
  374. DateNode node = new DateNode();
  375. node.setTime(format(date, FORMAT_MILLISECOND));
  376. node.setYear(calendar.get(Calendar.YEAR));
  377. node.setMonth(calendar.get(Calendar.MONTH) + 1);
  378. node.setDay(calendar.get(Calendar.DAY_OF_MONTH));
  379. node.setHour(calendar.get(Calendar.HOUR_OF_DAY));
  380. node.setMinute(calendar.get(Calendar.MINUTE));
  381. node.setSecond(calendar.get(Calendar.SECOND));
  382. node.setMillisecond(calendar.get(Calendar.MILLISECOND));
  383. node.setWeek(getWeek(calendar));
  384. node.setDayOfYear(calendar.get(Calendar.DAY_OF_YEAR));
  385. node.setWeekOfYear(getWeekOfYear(date));
  386. node.setWeekOfYearIgnoreLastYear(getWeekOfYearIgnoreLastYear(date));
  387. node.setMillisecondStamp(date.getTime());
  388. node.setSecondStamp(node.getMillisecondStamp() / 1000);
  389. return node;
  390. }
  391. /**
  392. * 日期变更
  393. *
  394. * @param date 指定日期
  395. * @param field 变更属性(如变更年份,则该值为 Calendar.DAY_OF_YEAR)
  396. * @param amount 变更大小(大于 0 时增加,小于 0 时减少)
  397. * @return 变更后的日期时间
  398. */
  399. public static Date add(Date date, int field, int amount) {
  400. if (Objects.isNull(date)) {
  401. return null;
  402. }
  403. Calendar calendar = Calendar.getInstance();
  404. calendar.setTime(date);
  405. calendar.add(field, amount);
  406. return calendar.getTime();
  407. }
  408. /**
  409. * 指定日期加减年份
  410. *
  411. * @param date 指定日期
  412. * @param year 变更年份(大于 0 时增加,小于 0 时减少)
  413. * @return 变更年份后的日期
  414. */
  415. public static Date addYear(Date date, int year) {
  416. return add(date, Calendar.YEAR, year);
  417. }
  418. /**
  419. * 指定日期加减月份
  420. *
  421. * @param date 指定日期
  422. * @param month 变更月份(大于 0 时增加,小于 0 时减少)
  423. * @return 变更月份后的日期
  424. */
  425. public static Date addMonth(Date date, int month) {
  426. return add(date, Calendar.MONTH, month);
  427. }
  428. /**
  429. * 指定日期加减天数
  430. *
  431. * @param date 指定日期
  432. * @param day 变更天数(大于 0 时增加,小于 0 时减少)
  433. * @return 变更天数后的日期
  434. */
  435. public static Date addDay(Date date, int day) {
  436. return add(date, Calendar.DAY_OF_YEAR, day);
  437. }
  438. /**
  439. * 指定日期加减星期
  440. *
  441. * @param date 指定日期
  442. * @param week 变更星期数(大于 0 时增加,小于 0 时减少)
  443. * @return 变更星期数后的日期
  444. */
  445. public static Date addWeek(Date date, int week) {
  446. return add(date, Calendar.WEEK_OF_YEAR, week);
  447. }
  448. /**
  449. * 指定日期加减小时
  450. *
  451. * @param date 指定日期时间
  452. * @param hour 变更小时数(大于 0 时增加,小于 0 时减少)
  453. * @return 变更小时数后的日期时间
  454. */
  455. public static Date addHour(Date date, int hour) {
  456. return add(date, Calendar.HOUR_OF_DAY, hour);
  457. }
  458. /**
  459. * 指定日期加减分钟
  460. *
  461. * @param date 指定日期时间
  462. * @param minute 变更分钟数(大于 0 时增加,小于 0 时减少)
  463. * @return 变更分钟数后的日期时间
  464. */
  465. public static Date addMinute(Date date, int minute) {
  466. return add(date, Calendar.MINUTE, minute);
  467. }
  468. /**
  469. * 指定日期加减秒
  470. *
  471. * @param date 指定日期时间
  472. * @param second 变更秒数(大于 0 时增加,小于 0 时减少)
  473. * @return 变更秒数后的日期时间
  474. */
  475. public static Date addSecond(Date date, int second) {
  476. return add(date, Calendar.SECOND, second);
  477. }
  478. /**
  479. * 指定日期加减秒
  480. *
  481. * @param date 指定日期时间
  482. * @param minute 变更毫秒数(大于 0 时增加,小于 0 时减少)
  483. * @return 变更毫秒数后的日期时间
  484. */
  485. public static Date addMillisecond(Date date, int millisecond) {
  486. return add(date, Calendar.MILLISECOND, millisecond);
  487. }
  488. /**
  489. * 获取该日期所在周指定星期的日期
  490. *
  491. * @param date 日期所在时间
  492. * @return index 指定星期(1 - 7 分别对应星期一到星期天)
  493. */
  494. public static Date getWeekDate(Date date, int index) {
  495. if (index < WEEK_1_MONDAY || index > WEEK_7_SUNDAY) {
  496. return null;
  497. }
  498. int week = getWeek(date);
  499. return addDay(date, index - week);
  500. }
  501. /**
  502. * 获取该日期所在周开始日期
  503. *
  504. * @param date 日期所在时间
  505. * @return 所在周开始日期
  506. */
  507. public static Date getWeekDateStart(Date date) {
  508. return getDateStart(getWeekDate(date, WEEK_1_MONDAY));
  509. }
  510. /**
  511. * 获取该日期所在周开始日期
  512. *
  513. * @param date 日期所在时间
  514. * @return 所在周开始日期
  515. */
  516. public static Date getWeekDateEnd(Date date) {
  517. return getWeekDateEnd(getWeekDate(date, WEEK_7_SUNDAY));
  518. }
  519. /**
  520. * 获取该日期所在周的所有日期(周一到周日)
  521. *
  522. * @param Date 日期
  523. * @return 该日照所在周的所有日期
  524. */
  525. public static List<Date> getWeekDateList(Date date) {
  526. if (Objects.isNull(date)) {
  527. return Collections.emptyList();
  528. }
  529. // 获取本周开始时间
  530. Date weekFromDate = getWeekDateStart(date);
  531. // 获取本周截止时间
  532. Date weekeEndDate = getWeekDateEnd(date);
  533. return getBetweenDateList(weekFromDate, weekeEndDate, true);
  534. }
  535. /**
  536. * 获取该日期所在周的所有日期(周一到周日)
  537. *
  538. * @param dateString
  539. * @return 该日照所在周的所有日期
  540. */
  541. public static List<String> getWeekDateList(String dateString) {
  542. Date date = parseDate(dateString);
  543. if (Objects.isNull(date)) {
  544. return Collections.emptyList();
  545. }
  546. return getDateStrList(getWeekDateList(date));
  547. }
  548. /**
  549. * 获取该日期所在月的所有日期
  550. *
  551. * @param dateString
  552. * @return 该日照所月的所有日期
  553. */
  554. public static List<Date> getMonthDateList(Date date) {
  555. if (Objects.isNull(date)) {
  556. return Collections.emptyList();
  557. }
  558. Date monthDateStart = getMonthDateStart(date);
  559. Date monthDateEnd = getMonthDateEnd(date);
  560. return getBetweenDateList(monthDateStart, monthDateEnd, true);
  561. }
  562. /**
  563. * 获取该日期所在月的所有日期
  564. *
  565. * @param dateString
  566. * @return 该日照所月的所有日期
  567. */
  568. public static List<String> getMonthDateList(String dateString) {
  569. Date date = parseDate(dateString);
  570. if (Objects.isNull(date)) {
  571. return Collections.emptyList();
  572. }
  573. return getDateStrList(getMonthDateList(date));
  574. }
  575. /**
  576. * 获取本日期所在月第一天
  577. *
  578. * @param date 日期
  579. * @return 本日期所在月第一天
  580. */
  581. public static Date getMonthDateStart(Date date) {
  582. if (Objects.isNull(date)) {
  583. return null;
  584. }
  585. Calendar calendar = Calendar.getInstance();
  586. calendar.setTime(date);
  587. calendar.set(Calendar.DAY_OF_MONTH, 1);
  588. return getDateStart(calendar.getTime());
  589. }
  590. /**
  591. * 获取本日期所在月最后一天
  592. *
  593. * @param date 日期
  594. * @return 本日期所在月最后一天
  595. */
  596. public static Date getMonthDateEnd(Date date) {
  597. if (Objects.isNull(date)) {
  598. return null;
  599. }
  600. Date monthDateStart = getMonthDateStart(date);
  601. Date nextMonthDateStart = getMonthDateStart(addMonth(monthDateStart, 1));
  602. return getDateEnd(addDay(nextMonthDateStart, -1));
  603. }
  604. /**
  605. * 获取两个日期相差的天数(以日期为单位计算,不以24小时制计算,详见下面说明)<br>
  606. *
  607. * 【说明】比如 2022-06-17 23:00:00 和 2022-06-17 01:00:00,两者虽然只相差 2 个小时,但也算相差 1 天 <br>
  608. *
  609. * @param date1 日期1
  610. * @param date2 日期2
  611. * @return 相差天数(若返回 -1,则至少有一个日期存在为空,此时不能进行比较)
  612. */
  613. public static int countBetweenDays(Date date1, Date date2) {
  614. if (Objects.isNull(date1) || Objects.isNull(date2)) {
  615. return -1;
  616. }
  617. // 获取两个日期 0 点 0 时 0 分 0 秒 0 毫秒时的时间戳(毫秒级)
  618. long t1 = getDateStart(date1).getTime();
  619. long t2 = getDateStart(date2).getTime();
  620. // 相差天数 = 相差的毫秒数 / 一天的毫秒数
  621. return (int) (Math.abs(t1 - t2) / DAY_MILLISECONDS);
  622. }
  623. /**
  624. * 获取两个日期之间的所有日期
  625. *
  626. * @param date1 日期1
  627. * @param date2 日期2
  628. * @return 两个日期之间的所有日期的开始时间
  629. */
  630. public static List<Date> getBetweenDateList(Date date1, Date date2) {
  631. return getBetweenDateList(date1, date2, false);
  632. }
  633. /**
  634. * 获取两个日期之间的所有日期
  635. *
  636. * @param date1 日期1
  637. * @param date2 日期2
  638. * @return 两个日期之间的所有日期的开始时间
  639. */
  640. public static List<Date> getBetweenDateList(Date date1, Date date2, boolean isContainParams) {
  641. if (Objects.isNull(date1) || Objects.isNull(date2)) {
  642. return Collections.emptyList();
  643. }
  644. // 确定前后日期
  645. Date fromDate = date1;
  646. Date toDate = date2;
  647. if (date2.before(date1)) {
  648. fromDate = date2;
  649. toDate = date1;
  650. }
  651. // 获取两个日期每天的开始时间
  652. Date from = getDateStart(fromDate);
  653. Date to = getDateStart(toDate);
  654. // 获取日期,开始循环
  655. List<Date> dates = new ArrayList<Date>();
  656. if (isContainParams) {
  657. dates.add(from);
  658. }
  659. Date date = from;
  660. boolean isBefore = true;
  661. while (isBefore) {
  662. date = addDay(date, 1);
  663. isBefore = date.before(to);
  664. if (isBefore) {
  665. dates.add(getDateStart(date));
  666. }
  667. }
  668. if (isContainParams) {
  669. dates.add(to);
  670. }
  671. return dates;
  672. }
  673. /**
  674. * 获取两个日期之间的所有日期
  675. *
  676. * @param dateString1 日期1(如:2022-06-20)
  677. * @param dateString2 日期2(如:2022-07-15)
  678. * @return 两个日期之间的所有日期(不包含参数日期)
  679. */
  680. public static List<String> getBetweenDateList(String dateString1, String dateString2) {
  681. return getBetweenDateList(dateString1, dateString2, false);
  682. }
  683. /**
  684. * 获取两个日期之间的所有日期
  685. *
  686. * @param dateString1 日期1(如:2022-06-20)
  687. * @param dateString2 日期2(如:2022-07-15)
  688. * @param isContainParams 是否包含参数的两个日期
  689. * @return 两个日期之间的所有日期的开始时间
  690. */
  691. public static List<String> getBetweenDateList(String dateString1, String dateString2, boolean isContainParams) {
  692. Date date1 = parseDate(dateString1);
  693. Date date2 = parseDate(dateString2);
  694. List<Date> dates = getBetweenDateList(date1, date2, isContainParams);
  695. return getDateStrList(dates);
  696. }
  697. /**
  698. * List<Date> 转 List<String>
  699. *
  700. * @param dates 日期集合
  701. * @return 日期字符串集合
  702. */
  703. public static List<String> getDateStrList(List<Date> dates) {
  704. if (dates.isEmpty()) {
  705. return Collections.emptyList();
  706. }
  707. List<String> dateList = new ArrayList<String>();
  708. for (Date date : dates) {
  709. dateList.add(formatDate(date));
  710. }
  711. return dateList;
  712. }
  713. static class DateNode {
  714. /** 年 */
  715. private int year;
  716. /** 月 */
  717. private int month;
  718. /** 日 */
  719. private int day;
  720. /** 时 */
  721. private int hour;
  722. /** 分 */
  723. private int minute;
  724. /** 秒 */
  725. private int second;
  726. /** 毫秒 */
  727. private int millisecond;
  728. /** 星期几( 1 - 7 对应周一到周日) */
  729. private int week;
  730. /** 当年第几天 */
  731. private int dayOfYear;
  732. /** 当年第几周(本年周 1 为第 1 周,0 则表示属于去年最后一周) */
  733. private int weekOfYear;
  734. /** 当年第几周(本年周 1 为第 1 周,0 则表示属于去年最后一周) */
  735. private int weekOfYearIgnoreLastYear;
  736. /** 时间戳(秒级) */
  737. private long secondStamp;
  738. /** 时间戳(毫秒级) */
  739. private long millisecondStamp;
  740. /** 显示时间 */
  741. private String time;
  742. public int getYear() {
  743. return year;
  744. }
  745. public void setYear(int year) {
  746. this.year = year;
  747. }
  748. public int getMonth() {
  749. return month;
  750. }
  751. public void setMonth(int month) {
  752. this.month = month;
  753. }
  754. public int getDay() {
  755. return day;
  756. }
  757. public void setDay(int day) {
  758. this.day = day;
  759. }
  760. public int getHour() {
  761. return hour;
  762. }
  763. public void setHour(int hour) {
  764. this.hour = hour;
  765. }
  766. public int getMinute() {
  767. return minute;
  768. }
  769. public void setMinute(int minute) {
  770. this.minute = minute;
  771. }
  772. public int getSecond() {
  773. return second;
  774. }
  775. public void setSecond(int second) {
  776. this.second = second;
  777. }
  778. public int getMillisecond() {
  779. return millisecond;
  780. }
  781. public void setMillisecond(int millisecond) {
  782. this.millisecond = millisecond;
  783. }
  784. public int getWeek() {
  785. return week;
  786. }
  787. public void setWeek(int week) {
  788. this.week = week;
  789. }
  790. public int getDayOfYear() {
  791. return dayOfYear;
  792. }
  793. public void setDayOfYear(int dayOfYear) {
  794. this.dayOfYear = dayOfYear;
  795. }
  796. public int getWeekOfYear() {
  797. return weekOfYear;
  798. }
  799. public void setWeekOfYear(int weekOfYear) {
  800. this.weekOfYear = weekOfYear;
  801. }
  802. public int getWeekOfYearIgnoreLastYear() {
  803. return weekOfYearIgnoreLastYear;
  804. }
  805. public void setWeekOfYearIgnoreLastYear(int weekOfYearIgnoreLastYear) {
  806. this.weekOfYearIgnoreLastYear = weekOfYearIgnoreLastYear;
  807. }
  808. public long getSecondStamp() {
  809. return secondStamp;
  810. }
  811. public void setSecondStamp(long secondStamp) {
  812. this.secondStamp = secondStamp;
  813. }
  814. public long getMillisecondStamp() {
  815. return millisecondStamp;
  816. }
  817. public void setMillisecondStamp(long millisecondStamp) {
  818. this.millisecondStamp = millisecondStamp;
  819. }
  820. public String getTime() {
  821. return time;
  822. }
  823. public void setTime(String time) {
  824. this.time = time;
  825. }
  826. }
  827. }

发表评论

表情:
评论列表 (有 0 条评论,210人围观)

还没有评论,来说两句吧...

相关阅读