//LocalDate、LocalTime、LocalDateTime LocalDateTime ldt = LocalDateTime.now(); System.out.println(ldt); //2019-08-13T22:30:11.982 LocalDateTime ldt2 = LocalDateTime.of(2015, 10, 19, 13, 22, 22); System.out.println(ldt2); //2015-10-19T13:22:22 LocalDateTime ldt3 = ldt.plusYears(2); System.out.println(ldt3); //2021-08-13T22:30:11.982 LocalDateTime ldt4 = ldt.minusMonths(2); System.out.println(ldt4); //2019-06-13T22:30:11.982 System.out.println(ldt.getYear()); //2019 System.out.println(ldt.getMonthValue()); //8 System.out.println(ldt.getDayOfMonth()); //13 System.out.println(ldt.getHour()); //22 System.out.println(ldt.getMinute()); //30 System.out.println(ldt.getSecond()); //11
Instant 时间戳(以unix元年:1970年1月1日00:00:00到某个时间之间的毫秒值)
// Instant 时间戳(以unix元年:1970年1月1日00:00:00到某个时间之间的毫秒值) Instant instant=Instant.now(); System.out.println(instant.toEpochMilli());
// Duration 用来计算两个时间之间的间隔
// Duration 用来计算两个时间之间的间隔 Instant instant=Instant.now(); Thread.sleep(1000); Instant instant1=Instant.now(); Duration duration=Duration.between(instant,instant1); System.out.println(duration.toMillis()); //1009
LocalTime localTime=LocalTime.now(); Thread.sleep(1000); LocalTime localTime1= LocalTime.now(); Duration duration=Duration.between(localTime,localTime1); System.out.println(duration.toMillis()); //1003
// Period 用来计算两个日期之间的间隔
LocalDate localDate = LocalDate.of(2015, 1, 1); LocalDate localDate1 = LocalDate.now(); Period between = Period.between(localDate, localDate1); System.out.println(between.getYears()); // 4 System.out.println(between.getMonths()); // 7 System.out.println(between.getDays()); // 12