广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Java时间戳类Instant的使用详解
  • 166
分享到

Java时间戳类Instant的使用详解

2024-04-02 19:04:59 166人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

目录前言Instant类时间点时间表示Instant实例化Instant获取参数Instant时间点比较Instant时间点运算前言 在JAVA8之前的版本,去获取时间戳(毫秒级别)

前言

在JAVA8之前的版本,去获取时间戳(毫秒级别)常用的办法有两种

// 方法一:构建日期Date类然后调用getTime方法
Date date = new Date();
System.out.println(date.getTime());

// 方法二:使用System类静态方法获取
System.out.println(System.currentTimeMillis());

由于Date类大部分方法已经废弃,而且上面两种方法的时间戳只能精确到毫秒级别,所以我们有必要了解下jdk1.8推出的Instant类,该类可以将时间戳精确到纳秒级别。

Instant类

时间点

该类对象表示的是时间线上的一点,这个时间点存在标准的UTC时间,注意这个时间并不是指北京时间或东京时间而是指世界时间

// 获取当前时间 2022-09-26T03:12:58.517Z(比当地时间相差8个小时)
System.out.println(Instant.now());

// 获取系统默认时间戳 2022-09-26T11:12:58.517+08:00[Asia/Shanghai]
System.out.println(Instant.now().atZone(ZoneId.systemDefault()));

在Instant时间线上存在三个重要的点位,最大点、最小点、原点也就是说小于1970-01-01的时间戳就为负数,超过1970-01-01的时间戳就为正数

// 时间线上最大点  +1000000000-12-31T23:59:59.999999999Z
System.out.println(Instant.MAX);

// 时间线上最小点  -1000000000-01-01T00:00:00Z
System.out.println(Instant.MIN);

// 时间线上原点   1970-01-01T00:00:00Z
System.out.println(Instant.EPOCH);

// 输出结果为-8369623
System.out.println(Instant.parse("1969-09-26T03:06:17.323Z").getEpochSecond());

时间表示

在Instant中采用两个字段表示时间戳


private final long seconds;

private final int nanos;

Instant实例化

普通实例化分为如下几种

// 获取当前时间
Instant instant1 = Instant.now();

// 字符串转Instant
Instant instant2 = Instant.parse("2022-09-26T03:46:24.373Z");

// 构建秒级Instant对象,从时间1970-01-01T00:00:00Z开始计算(距离原点5000秒)
// 结果为:1970-01-01T01:23:20Z
Instant instant3 = Instant.ofEpochSecond(5000);

// 构建毫秒级Instant对象,同样从时间1970-01-01T00:00:00Z开始计算(距离原点5000毫秒)
// 结果为:1970-01-01T00:00:05Z
Instant instant4 = Instant.ofEpochMilli(5000);

还有一种特殊的如下,可以构建纳秒级的Instant对象

// 构建纳秒级Instant对象,同样从时间1970-01-01T00:00:00Z开始计算
// 参数:epochSecond(秒),nanoAdjustment(纳秒)
// 结果为:1970-01-01T00:00:05.000001111Z
Instant instant5 = Instant.ofEpochSecond(5, 1111);

不过我们需要注意Instant.ofEpochSecond方法的源码,如下

static final long NANOS_PER_SECOND = 1000_000_000L;

public static Instant ofEpochSecond(long epochSecond, long nanoAdjustment) {
    // Math.floorDiv是除法运算,返回小于或等于商的整数 Math.floorDiv(25, 3)=8
    // Math.addExact加法运算,Math.addExact(1, 2)=3
    long secs = Math.addExact(epochSecond, Math.floorDiv(nanoAdjustment, NANOS_PER_SECOND));
    // Math.floORMod是模运算,Math.floorMod(9, 20)=9
    int nos = (int)Math.floorMod(nanoAdjustment, NANOS_PER_SECOND);
    return create(secs, nos);
}

Instant获取参数

Instant instant = Instant.now();
// 时区相差8小时 2022-09-26T07:04:19.110Z
System.out.println(instant);

System.out.println("秒:"+instant.getEpochSecond());

System.out.println("毫秒:"+instant.toEpochMilli());
// 1毫秒 = 1000 000 纳秒
System.out.println("纳秒:"+instant.getNano());

Instant时间点比较

由于时间点位于时间线上,所以可以直接进行对比。

Instant instant1 = Instant.parse("2022-09-26T07:04:19.110Z");
Instant instant2 = Instant.parse("2022-09-26T07:04:19.110Z");
Instant instant3 = Instant.parse("2022-08-26T07:04:19.110Z");

// 相等为0
System.out.println(instant1.compareTo(instant2));
// instant1大于instant3 为1
System.out.println(instant1.compareTo(instant3));
// instant1小于instant3 为-1
System.out.println(instant3.compareTo(instant1));

// true
System.out.println(instant1.isAfter(instant3));
// false
System.out.println(instant1.isBefore(instant3));

Instant时间点运算

Instant instant1 = Instant.parse("2022-09-26T07:04:19.110Z");

// 在instant1的基础上增加2秒,值为:2022-09-26T07:04:21.110Z
System.out.println(instant1.plusSeconds(2));

// 在instant1的基础上增加1毫秒,值为:2022-09-26T07:04:19.111Z
System.out.println(instant1.plusMillis(1));

// 在instant1的基础上增加1001纳秒,值为:2022-09-26T07:04:19.110001001Z
System.out.println(instant1.plusNanos(1001));

// 在instant1的基础上增加1秒,值为:2022-09-26T07:04:20.110Z
// 该值取决于后面指定的单位,可以从ChronoUnit枚举类获取
System.out.println(instant1.plus(1, ChronoUnit.SECONDS));

// 在instant1的基础上减去1秒,值为:2022-09-26T07:04:18.110Z
// plus是增加,minus是减少,逻辑类似可以参考上面plus相关A
System.out.println(instant1.minusSeconds(1));

Instant时间点计算时需要注意,无论是调用plus或者minus相关API都会重新创建新对象。

到此这篇关于Java时间戳类Instant的使用详解的文章就介绍到这了,更多相关Java时间戳类Instant内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Java时间戳类Instant的使用详解

本文链接: https://www.lsjlt.com/news/168671.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作