JavaSE常用类之Instant类及常用方法

时间:2021-03-09 13:52:20   收藏:0   阅读:0

Instant 类的使用

Instant 类概述

在时间线上的瞬间点。该类在时间线上建立单个瞬时点。 这可能用于在应用程序中记录事件时间戳。

now( ) 方法

//now() 获取本初子午线的时间
Instant instant = Instant.now();
System.out.println(instant);      //中时区		2021-03-09T02:02:46.596Z

atOffset( ) 方法添加时间偏移量

//添加时间偏移量
OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
System.out.println(offsetDateTime);       //东八区		2021-03-09T10:02:46.596+08:00

toEpochMilli( ) 方法获取时间戳

long milli = instant.toEpochMilli();
System.out.println(milli);		//1615255366596

ofEpochMilli( ) 方法通过时间戳获取时间

//通过时间戳获取时间 ----- 本初子午线的时间
Instant instant1 = Instant.ofEpochMilli(1615198793505L);
System.out.println(instant1);		//2021-03-08T10:19:53.505Z
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!