前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >如何构造一个 Instant

如何构造一个 Instant

原创
作者头像
张紫娃
修改2024-07-31 08:03:18
2770
修改2024-07-31 08:03:18
举报
文章被收录于专栏:张紫娃的学习笔记

啥是Instant?

An instantaneous point on the time-line. 中文描述就是Instant表示高精度时间戳。

为何引入Instant

1. 现代化升级

java.time.Instant 类是在 Java 8 中引入,是对原有日期时间处理API(主要包括 java.util.Date、java.util.Calendar 和 java.sql.Timestamp 等类)的重大改进和现代化升级。

2. 纳秒级精度

3. Instant 所有实例都是不可变的,线程安全。


Java里如何构造Instant

1Instant.now →【时间戳 的 0时区时间格式】

代码语言:java
复制
System.out.println(ZonedDateTime.now().format(DateTimeFormatter.ISO_DATE_TIME));
// 于2024-07-30T14:50:13.0489927+08:00[Asia/Shanghai]执行如下代码

System.out.println(Instant.now());                                   // 2024-07-30T06:50:13.053659800Z 【时间戳显示成0时区时间格式】
System.out.println(Instant.now().toString());                        // 2024-07-30T06:50:13.053819900Z 【时间戳显示成0时区时间格式】
System.out.println(Instant.now().atZone(ZoneOffset.UTC));            // 2024-07-30T06:50:13.053819900Z                  【时间戳显示成指定时区时间格式】
System.out.println(Instant.now().atZone(ZoneId.of("Asia/Tokyo")));   // 2024-07-30T15:50:13.053819900+09:00[Asia/Tokyo] 【时间戳显示成指定时区时间格式】

System.out.println(System.currentTimeMillis());            // 1722322213053
System.out.println(Instant.now().toEpochMilli());          // 1722322213053
System.out.println(Instant.now().getEpochSecond());        // 1722322213
System.out.println(Instant.now().getNano());               // 53819900

ofEpoch →【 时间戳 的 0时区时间格式】

代码语言:java
复制
Instant.ofEpochMilli(1562501898000L));  //2019-07-07T12:18:18Z             【0时区时间】
Instant.ofEpochMilli(1562501898888L));  //2019-07-07T12:18:18.888Z         【0时区时间】
Instant.ofEpochSecond(1562501898));     //2019-07-07T12:18:18Z             【0时区时间】
Instant.ofEpochSecond(1562501898,888)); //2019-07-07T12:18:18.000000888Z   【0时区时间】

parse → 【0时区时间 的 String → Date】

代码语言:java
复制
Instant.parse("2019-07-07T20:18:18.000000888Z"));  //2019-07-07T20:18:18.000000888Z 【视String为0时区时间去类型转换】
Instant.parse("2019-07-07T20:18:18Z"));            //2019-07-07T20:18:18Z           【视String为0时区时间去类型转换】

toInstant(存在时区转换)【根据日期 获取 时间戳】

代码语言:java
复制
ZonedDateTime ZONED_DATE_TIME = ZonedDateTime.of(2019, 7, 7, 20, 18, 18, 888, ZoneId.of("Asia/Tokyo"));
OffsetDateTime OFFSET_DATE_TIME = OffsetDateTime.of(2019, 7, 7, 20, 18, 18, 888, ZoneOffset.ofHours(9));
LocalDateTime LOCAL_DATE_TIME = LocalDateTime.of(2019, 7, 7, 20, 18, 18, 888);
Date DATE = new Date(1562501898888L);// Sun Jul 07 20:18:18 CST 2019

ZONED_DATE_TIME.toInstant());                      //2019-07-07T11:18:18.000000888Z 【0时区】
OFFSET_DATE_TIME.toInstant());                     //2019-07-07T11:18:18.000000888Z 【0时区】
LOCAL_DATE_TIME.toInstant(ZoneOffset.ofHours(8))); //2019-07-07T12:18:18.000000888Z 【0时区】
LOCAL_DATE_TIME.toInstant(ZoneOffset.UTC));        //2019-07-07T20:18:18.000000888Z 【0时区】
DATE.toInstant());                                 //2019-07-07T12:18:18.888Z       【0时区】

特殊Instant

代码语言:java
复制
Instant.MIN);//-1000000000-01-01T00:00:00Z
Instant.MAX);//+1000000000-12-31T23:59:59.999999999Z
Instant.EPOCH);//1970-01-01T00:00:00Z

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 啥是Instant?
  • 为何引入Instant?
    • 1. 现代化升级
      • 2. 纳秒级精度
        • 3. Instant 所有实例都是不可变的,线程安全。
        • Java里如何构造Instant?
          • 1Instant.now →【时间戳 的 0时区时间格式】
            • ofEpoch →【 时间戳 的 0时区时间格式】
              • parse → 【0时区时间 的 String → Date】
                • toInstant(存在时区转换)【根据日期 获取 时间戳】
                  • 特殊Instant
                  领券
                  问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档