/*
Instant的使用
类似于java.util.Date类
*/
@Test
public void test2(){
//now():获取本初子午线对应的标准时间
Instant instant = Instant.now();
System.out.println(instant);//2022-04-25T05:56:58.562637400Z
//添加时间的偏移量
OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
System.out.println(offsetDateTime);//2022-04-25T14:04:52.981486300+08:00
//toEpochMilli():获取自1970年1月1日0时0分0秒(UTC)开始的毫秒数 ---> Date类的getTime()
long milli = instant.toEpochMilli();
System.out.println(milli);//1650867450064
//ofEpochMilli():通过给定的毫秒数,获取Instant实例 ---> Date(long millis)
Instant instant1 = Instant.ofEpochMilli(1650867450064L);
System.out.println(instant1);//2022-04-25T06:17:30.064Z
}
[常用类]Instant类的使用
最新推荐文章于 2025-05-25 14:42:41 发布
本文详细介绍了Java中Instant类的使用,包括获取当前标准时间、时间偏移、转换为毫秒和Epoch Milli,以及从毫秒创建Instant实例。

2721

被折叠的 条评论
为什么被折叠?



