LocalDateTime
LocalDateTime now = LocalDateTime.now();
LocalDateTime time = LocalDateTime.of(2020, 1, 1, 12,0);
LocalDateTime newTime = time.plusHours(2);
now.getYear()
Instant
Instant instant = Instant.now();
System.out.println(instant);
OffsetDateTime localInstant = instant.atOffset(ZoneOffset.ofHours(8));
System.out.println(localInstant);
duration——毫秒间隔
Instant ins1 = Instant.now();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Instant ins2 = Instant.now();
Duration duration = Duration.between(ins1, ins2);
System.out.println(duration.getSeconds());
period——日期间隔
LocalDate now = LocalDate.now();
LocalDate time = LocalDate.of(2020, 1, 1);
Period period = Period.between(now, time);
System.out.println(period);
获取下周一
LocalDateTime time = now.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
DateTimeFormatter——格式化
DateTimeFormatter date = DateTimeFormatter.ISO_DATE;
DateTimeFormatter time = DateTimeFormatter.ISO_TIME;
LocalDateTime now = LocalDateTime.now();
String date_str = now.format(date);
String time_str = now.format(time);
System.out.println(date_str);
System.out.println(time_str);
DateTimeFormatter selfFormat = DateTimeFormatter.ofPattern("yyyy年MM月dd日HH时mm分ss秒");
String format = now.format(selfFormat);
System.out.println(format);