jdk1.8新增的三个日期类

  • LocalDate
  • LocalTime
  • LocalDateTime

以及格式化类(线程安全)

private static DateTimeFormatter dateFormatter = DateTiemFormatter.ofPattern(“yyyy-M-d”);

  1. package basics;
  2. import java.time.*;
  3. import java.time.format.DateTimeFormatter;
  4. import java.time.temporal.ChronoUnit;
  5. import java.time.temporal.TemporalAdjusters;
  6. import java.util.Date;
  7. /**
  8. * @author Created by yinrg on 2022年3月25日, 0025 下午 5:04
  9. * @fileSimpleName DateDemo2
  10. * @fileName basics.DateDemo2
  11. * @description JDK1.8新增的时间API
  12. */
  13. public class DateDemo2 {
  14. private static DateTimeFormatter dateTimeFormatter01 = DateTimeFormatter.ofPattern("yyyy-M-d");
  15. private static DateTimeFormatter dateTimeFormatter02 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
  16. private static DateTimeFormatter dateTimeFormatter03 = DateTimeFormatter.ofPattern("yyyy-M-d H:m:s");
  17. public static void main(String[] args) {
  18. /**
  19. * parse() 使用特定格式化程序从文本字符串中获取LocalDate的实例。
  20. * 使用格式化程序解析文本,返回日期。
  21. */
  22. LocalDate ld01 = LocalDate.parse("2022-03-01");
  23. LocalDate ld02 = LocalDate.parse("2022-03-9", dateTimeFormatter01);
  24. LocalDate ld03 = LocalDate.parse("2022-03-01", dateTimeFormatter02);
  25. System.out.println(ld01);
  26. System.out.println(ld02);
  27. System.out.println(ld03);
  28. // 格式化操作
  29. System.out.println(dateTimeFormatter01.format(ld01));
  30. System.out.println(dateTimeFormatter01.format(ld02));
  31. System.out.println(dateTimeFormatter02.format(ld03));
  32. System.out.println(daysBetween(ld01, ld02));
  33. System.out.println(weekSunday(ld01));
  34. System.out.println(weekMonday(ld02));
  35. System.out.println(monthEnd(ld02));
  36. System.out.println(monthStart(ld02));
  37. // LocalDateTime
  38. System.out.println(LocalDateTime.parse("2022-3-21 11:11:11", dateTimeFormatter03)); // 2022-03-21T11:11:11
  39. /**
  40. * LocalDateTime与Date之间的互相转换
  41. * new Date().getTime() 是Date转Long
  42. * new Date(long time) 是long转Date
  43. */
  44. long nowMillisecond = getMillisecond(LocalDateTime.now());
  45. System.out.println(new Date(nowMillisecond));
  46. }
  47. /**
  48. * LocalDateTime转Long
  49. * @param dateTime
  50. * @return
  51. */
  52. public static long getMillisecond(LocalDateTime dateTime){
  53. return dateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
  54. }
  55. /**
  56. * Long转LocalDateTime
  57. * @param timestamp
  58. * @return
  59. */
  60. public static LocalDateTime parse(Long timestamp){
  61. Instant instant = Instant.ofEpochMilli(timestamp);
  62. ZoneId zone = ZoneId.systemDefault();
  63. return LocalDateTime.ofInstant(instant,zone);
  64. }
  65. /**
  66. * 获取两个日期之间差值
  67. *
  68. * @param from
  69. * @param to
  70. * @return
  71. */
  72. public static long daysBetween(LocalDate from, LocalDate to) {
  73. return from.until(to, ChronoUnit.DAYS);
  74. }
  75. /**
  76. * 获取所在周日是哪一天
  77. *
  78. * @param date
  79. * @return
  80. */
  81. public static LocalDate weekSunday(LocalDate date) {
  82. return date.plusDays(7 - date.getDayOfWeek().getValue());
  83. }
  84. /**
  85. * 获取所在周一是哪一天
  86. *
  87. * @param date
  88. * @return
  89. */
  90. public static LocalDate weekMonday(LocalDate date) {
  91. DayOfWeek dayOfWeek = date.getDayOfWeek();
  92. return date.minusDays(dayOfWeek.getValue() - 1);
  93. }
  94. /**
  95. * 获取所在月末是哪一天
  96. *
  97. * @param date
  98. * @return
  99. */
  100. public static LocalDate monthEnd(LocalDate date) {
  101. return date.with(TemporalAdjusters.lastDayOfMonth());
  102. }
  103. /**
  104. * 获取所在月初是哪一天
  105. *
  106. * @param date
  107. * @return
  108. */
  109. public static LocalDate monthStart(LocalDate date) {
  110. return LocalDate.of(date.getYear(), date.getMonth(), 1);
  111. }
  112. }