1、人日常工作用的是年月日小时分钟秒,程序的某个事件记录可能需要毫秒
即时间戳的应用
2、Instance 与Date类似,可精确到纳秒
3、代码查验学习理解
package com.test;
import org.junit.Test;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
public class TestInstance {
@Test
public void test(){
//获取子午线对应的标志时间
Instant now1 = Instant.now();
System.out.println("与北京时间差8个小时的哦:"+now1);
//根据时期添加偏移量
OffsetDateTime offsetDateTime1 = now1.atOffset(ZoneOffset.ofHours(8));
System.out.println("处理后时间为:"+offsetDateTime1);
//获取毫秒数
long l1 = now1.toEpochMilli();
//从1970 年开始的毫秒数
System.out.println("毫秒数为:"+l1);
}
}
运行结果
与北京时间差8个小时的哦:2022-01-24T04:39:38.730Z
处理后时间为:2022-01-24T12:39:38.730+08:00
毫秒数为:1642999178730