package com;
import java.time.*;
public class Main1 {
public static void main(String[] args) throws InterruptedException {
//本地时间
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println("localDateTime = " + localDateTime);
LocalDateTime of = LocalDateTime.of(2022, 1, 11, 12, 19);
System.out.println("of = " + of);
LocalDateTime localDateTime1 = localDateTime.plusYears(2);//加两年
System.out.println("加两年:localDateTime1 = " + localDateTime1);
int year = localDateTime.getYear();
System.out.println("year = " + year);
System.out.println("========================");
//时间戳 Instant 以unix元年(1970)到某个时间的毫秒值
Instant instant = Instant.now();
System.out.println("instant = " + instant);//默认获取的是 UTC 时区
OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));//中国时区
System.out.println(offsetDateTime);
System.out.println(instant.toEpochMilli());
Instant instant1 = Instant.ofEpochSecond(1000);
System.out.println("instant1 = " + instant1);
System.out.println("======================");
//Duration:计算两个时间戳之间的间隔
Instant x1 = Instant.now();
Thread.sleep(1000);
Instant x2 = Instant.now();
Duration duration = Duration.between(x1, x2);
System.out.println("duration = " + duration);
System.out.println("duration.toMillis() = " + duration.toMillis());
System.out.println("=========================");
LocalTime t1 = LocalTime.now();
Thread.sleep(1000);
LocalTime t2 = LocalTime.now();
Duration duration1 = Duration.between(t1, t2);
System.out.println("duration1.toMillis() = " + duration1.toMillis());
System.out.println("===========================");
//Period:计算两个日期之间的间隔
LocalDate of1 = LocalDate.of(2000, 1, 1);
LocalDate of2 = LocalDate.now();
Period period = Period.between(of1, of2);
System.out.println("period = " + period);
System.out.println("period.getYears() = " + period.getYears());
System.out.println("period.getMonths() = " + period.getMonths());
System.out.println("period.getDays() = " + period.getDays());
}
}