常用类17-Instant类的使用

基本概念

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

}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容