1. //1. LocalDate、LocalTime、LocalDateTime
    2. LocalDate localDate = LocalDate.now();
    3. System.out.println("localDate = " + localDate);// localDate = 2021-06-02
    4. LocalTime localTime = LocalTime.now();// localTime = 21:44:54.806
    5. System.out.println("localTime = " + localTime);
    6. LocalDateTime localDateTime = LocalDateTime.now();// localDateTime = 2021-06-02T21:44:54.806
    7. System.out.println("localDateTime = " + localDateTime);
    8. // 自定义时间
    9. LocalDate diyDate = LocalDate.of(2021, 12, 12);// diyDate = 2021-12-12
    10. System.out.println("diyDate = " + diyDate);
    11. //LocalDateTime diyLocalDateTime = LocalDateTime.of(LocalDate.of(2021, 6, 18), LocalTime.of(23, 23, 23));
    12. LocalDateTime diyLocalDateTime = LocalDateTime.of(localDate, localTime);// diyLocalDateTime = 2021-06-18T23:23:23
    13. System.out.println("diyLocalDateTime = " + diyLocalDateTime);
    14. // 时间的 ±
    15. LocalDate plus3Years = diyDate.plusYears(3);// plus3Years = 2024-12-12
    16. System.out.println("plus3Years = " + plus3Years);
    17. // 获取年月日,其中特别需要注意获取"月份"的差异
    18. int year = localDate.getYear();// year = 2021
    19. System.out.println("year = " + year);
    20. Month month = localDate.getMonth();
    21. System.out.println("localDate.getMonth() = " + month);// localDate.getMonth() = JUNE
    22. int monthValue = localDate.getMonthValue();// localDate.getMonthValue() = 6
    23. System.out.println("localDate.getMonthValue() = " + monthValue);
    24. //////////////////////////////////////////////
    25. //2. Instant : 时间戳。 (使用 Unix 元年 1970年1月1日 00:00:00 所经历的毫秒值)
    26. Instant now = Instant.now();
    27. System.out.println("now = " + now);
    28. //////////////////////////////////////////////
    29. // 3. 计算时间间隔
    30. // Duration : 用于计算两个“时间”间隔
    31. // Period : 用于计算两个“日期”间隔
    32. Period between = Period.between(diyDate, plus3Years);
    33. int years = between.getYears();
    34. System.out.println(diyDate + " 和 " + plus3Years + "之间的时间间隔(年):" + years);// 3
    35. int months = between.getMonths();
    36. System.out.println(diyDate + " 和 " + plus3Years + "之间的时间间隔(年):" + months);// 0,月份相同,所以为0。
    37. Instant now2 = Instant.now();
    38. System.out.println("now2 = " + now2);
    39. Duration duration = Duration.between(now, now2);
    40. long millis = duration.toMillis();
    41. System.out.println(now + " 和 " + now2 + " 的时间间隔(毫秒):" + millis);
    42. //////////////////////////////////////////////
    43. //4. TemporalAdjuster、TemporalAdjusters:时间校正器
    44. LocalDate tempDate = localDate.of(2021, 6, 12);// tempDate = 2021-06-12
    45. System.out.println("tempDate = " + tempDate);
    46. // 经典问题1:获取当前日期所在的周一和周日。
    47. // 有可能今天就是周一
    48. if (tempDate.getDayOfWeek().equals(DayOfWeek.MONDAY)) {
    49. System.out.println(tempDate + " 是周一");
    50. } else {
    51. System.out.println(tempDate + " 不是周一");
    52. LocalDate previousMonday = tempDate.with(TemporalAdjusters.previous(DayOfWeek.MONDAY));
    53. System.out.println("本周周一是 " + previousMonday);// 2021-6-7
    54. }
    55. // 获取当前日期所在的周日
    56. if (tempDate.getDayOfWeek().equals(DayOfWeek.SUNDAY)) {
    57. System.out.println(tempDate + " 是周日");
    58. } else {
    59. System.out.println(tempDate + " 不是周日");
    60. LocalDate nextSunday = tempDate.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));// nextSunday = 2021-06-13
    61. System.out.println("本周周日是 " + nextSunday);
    62. }
    63. // 经典问题2:获取本月第一天。
    64. LocalDate firstDayOfMonth = tempDate.with(TemporalAdjusters.firstDayOfMonth());// firstDayOfMonth = 2021-06-01
    65. System.out.println("firstDayOfMonth = " + firstDayOfMonth);
    66. //////////////////////////////////////////////
    67. // 5. 日期格式化 DateTimeFormatter
    68. DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    69. DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    70. String formatDate = localDate.format(dateFormatter);// formatDate = 2021-06-02
    71. System.out.println("formatDate = " + formatDate);
    72. String formatDateTime = localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));// formatDateTime = 2021-06-02 23:41:39
    73. System.out.println("formatDateTime = " + formatDateTime);
    74. LocalDate parseDate = LocalDate.parse("2021-02-11", dateFormatter);// parseDate = 2021-02-11
    75. System.out.println("parseDate = " + parseDate);
    76. LocalDateTime parseDateTime = LocalDateTime.parse("2021-06-02 23:48:06", dateTimeFormatter);// parseDateTime = 2021-06-02T23:48:06
    77. System.out.println("parseDateTime = " + parseDateTime);