LocalDateTime类用来处理日期和时间,该类对象实际是LocalDate和LocalTime对象的组合,它用来表示一个特定事件的开始时间等,如北京奥林匹克运动会开幕时间是2008年8月8日下午8点。7.9.3LocalDateTime类除了now()方法外,LocalDateTime类还提供了of()方法创建对象,如下所示。staticLocalDateTimenow():获得默认时区系统时钟当前日期和时间对象。staticLocalDateTimeof(int?year,int?month,int?dayOfMonth,int?hour,int?minute):通过指定的年月日和时分获得日期时间对象,秒和纳秒设置为0。varlocalTime=LocalDateTime.now();System.out.printf(现在时间:%s%n,localTime);System.out.printf(2024年10月6日11:30am:%s%n,LocalDateTime.of(2024,Month.OCTOBER,6,11,30));//从当前时刻获得当前日期时间varldt=LocalDateTime.ofInstant(Instant.now(),ZoneId.systemDefault());System.out.printf(现在时刻:%s%n,ldt);//当前时间的6个月之后和6个月之前的时间System.out.printf(6个月之后时间:%s%n,localTime.plusMonths(6));System.out.printf(6个月之前时间:%s%n,localTime.minusMonths(6));程序7-15DateTimeDemo.javaInstant类表示时间轴上的一个点。Duration类和Period类都表示一段时间,前者是基于时间的(秒,纳秒),后者是基于日期的(年、月、日)。7.9.4Instant类、Duration类和Period类时间轴上的原点是格林尼治时间1970年1月1日0点(1970-01-01T00:00:00Z)。从那一时刻开始时间按照每天86400秒精确计算,向前向后都以纳秒计算。Instant值可以追溯到10亿年前(Instant.MIN),最大值Instant.MAX是1000000000年12月31日。1.Instant类静态方法Instant.now()返回当前的瞬间时间点。Instanttimestamp=Instant.now();调用Instant类的toString()实例方法返回结果如下:2022-10-08T03:11:59.182ZInstant类定义了一些实例方法,如加减时间,下面代码在当前时间上加1小时:InstantoneHourLater=Instant.now().plusSeconds(60*60);1.Instant类Instant类还定义了isAfter()、isBefore()方法比较两个Instant实例。finalClockclock=Clock.systemUTC();//返回系统时钟时刻Instantinstant=clock.instant();Instantnow=Instant.now().plusSeconds(5);System.out.println(instant.isBefore(now));//输出truePeriod类表示基于日期的(年、月、日)一段时间,该类提供了getDays()、getMonths()、getYears()等方法从Period中抽取一段时间。整个时间段用年、月、日表示,如果要用单一时间单位表示,可以使用ChronoUnit.between()方法。2.Period类假设你的出生日期是2002年1月1日,下面代码可计算你的年龄。vartoday=LocalDate.now();varbirthday=LocalDate.of(2002,Month.JANUARY,1);Periodp=Period.between(birthday,today);//计算两个日期之间相差的