基本概念
Instant:常被用来记录应用程序中事件的时间戳,可以精确到纳秒级别
常用方法
now()、toEpochMilli()、ofEpochMilli()
@Test
public void testInstant(){
System.out.println("*****************获取当前时间:now()****************");
Instant instant = Instant.now();
//默认是本初子午线的时间,与中国时区相差8个小时,比当前时间早8个小时
System.out.println(instant);//2022-02-06T23:43:56.474Z
System.out.println("***************设置偏移量:atOffset()****************");
//添加偏移量
OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
System.out.println(offsetDateTime);//2022-02-07T07:43:56.474+08:00
//获取对应毫秒数:自1970年1月1日0时0分0秒开始的毫秒数(UTC)
System.out.println("**************获取毫秒数:toEpochMilli()*************");
System.out.println("自1970年1月1日0时0分0秒开始的毫秒数(UTC): " + instant.toEpochMilli());// 1644191222122
System.out.println("**************获取指定时间:ofEpochMilli()************");
Instant instant1 = Instant.ofEpochMilli(1644191222122L);
System.out.println(instant1);//2022-02-06T23:47:02.122Z
}