1. //获取今天的日期
    2. LocalDate today = LocalDate.now(); //2021-05-17
    3. //获取年、月、日信息
    4. LocalDate today = LocalDate.now(); //2021-05-17
    5. int year = today.getYear(); //2021
    6. int month = today.getMonthValue();//5
    7. int day = today.getDayOfMonth();//17
    8. //处理特定日期
    9. LocalDate dateOfBirth = LocalDate.of(2021, 05, 17);
    10. //判断两个日期是否相等
    11. LocalDate date1 = LocalDate.of(2021, 05, 14);
    12. LocalDate today = LocalDate.now();
    13. boolean result= date1.equals(today);
    14. //在Java 8中检查像生日这种周期性事件
    15. LocalDate today = LocalDate.now();
    16. LocalDate dateOfBirth = LocalDate.of(2010, 01, 14);
    17. MonthDay birthday = MonthDay.of(dateOfBirth.getMonth(), dateOfBirth.getDayOfMonth());
    18. MonthDay currentMonthDay = MonthDay.from(today);
    19. boolean result = currentMonthDay.equals(birthday)
    20. //计算一周后的日期
    21. LocalDate nextWeek = today.plus(1, ChronoUnit.WEEKS);
    22. //计算一年前或一年后的日期
    23. LocalDate previousYear = today.minus(1, ChronoUnit.YEARS);//一年前
    24. LocalDate nextYear = today.plus(1, YEARS);//一年后
    25. //用Java判断日期是早于还是晚于另一个日期
    26. LocalDate tomorrow = LocalDate.of(2014, 1, 15);
    27. if(tommorow.isAfter(today)){
    28. System.out.println("Tomorrow comes after today");
    29. }
    30. LocalDate yesterday = today.minus(1, DAYS);
    31. if(yesterday.isBefore(today)){
    32. System.out.println("Yesterday is day before today");}
    33. //在Java 8中处理时区
    34. oneId america = ZoneId.of("America/New_York");
    35. LocalDateTime localtDateAndTime = LocalDateTime.now();
    36. ZonedDateTime dateAndTimeInNewYork = ZonedDateTime.of(localtDateAndTime, america );
    37. //在Java 8中检查闰年
    38. LocalDate today = LocalDate.now();
    39. today.isLeapYear()
    40. //计算两个日期之间的天数和月数
    41. LocalDate java8Release = LocalDate.of(2014, Month.MARCH, 14);
    42. Period periodToNextJavaRelease = Period.between(today, java8Release);
    43. periodToNextJavaRelease.getMonths();//月数
    44. periodToNextJavaRelease.getDays() ; //天数
    45. periodToNextJavaRelease.getYears(); //年数
    46. -----------------------------------------------------------------------------------------------------
    47. //获取当前时间
    48. LocalTime time = LocalTime.now(); //19:49:18.235
    49. //在现有的时间上增加小时
    50. LocalTime time = LocalTime.now();
    51. LocalTime newTime = time.plusHours(2); // 在当前时间增加小时、分、秒
    52. LocalTime localTime = time.withHour(10);// 在当前时间减少时、分、秒
    53. //获取当前的时间戳
    54. Instant timestamp = Instant.now()
    55. ------------------------------------------------------------------------------------------------------
    56. //在Java 8中使用预定义的格式化工具去解析或格式化日期
    57. String dayAfterTommorrow = "20140116";
    58. LocalDate formatted = LocalDate.parse(dayAfterTommorrow,DateTimeFormatter.BASIC_ISO_DATE);
    59. String goodFriday = "Apr 18 2014";
    60. try {
    61. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM dd yyyy");
    62. LocalDate holiday = LocalDate.parse(goodFriday, formatter);
    63. System.out.printf("Successfully parsed String %s, date is %s%n", goodFriday, holiday);
    64. } catch (DateTimeParseException ex) {
    65. System.out.printf("%s is not parsable!%n", goodFriday);
    66. ex.printStackTrace();
    67. }
    68. //日期转换成字符串
    69. LocalDateTime arrivalDate = LocalDateTime.now();
    70. try {
    71. DateTimeFormatter format = DateTimeFormatter.ofPattern("MMM dd yyyy hh:mm a");
    72. String landing = arrivalDate.format(format);
    73. System.out.printf("Arriving at : %s %n", landing);
    74. } catch (DateTimeException ex) {
    75. System.out.printf("%s can't be formatted!%n", arrivalDate);
    76. ex.printStackTrace();
    77. }