目录
- 前言
- Instant类
- 时间点
- 时间表示
- Instant实例化
- Instant获取参数
- Instant时间点比较
- Instant时间点运算
前言
在 JAVA 8 之前的版本中,获取毫秒级时间戳通常依赖两种主流方案。
// 方法一:构建日期Date类然后调用getTime方法
Date date = new Date();
System.out.println(date.getTime());
// 方法二:使用System类静态方法获取
System.out.println(System.currentTimeMillis());
鉴于 Date 类中大量方法已被标记为废弃,且上述传统手段仅能精确至毫秒,引入 JDK 1.8 提供的 Instant 类显得尤为必要。该核心类能够将时间精度提升至纳秒级别,满足更严苛的时间处理需求。
Instant类
时间点
Instant 对象代表时间轴上的一个特定点位,其基准为标准 UTC 时间。需明确的是,此时间体系独立于北京时间或东京时间等本地时区,纯粹指向世界统一时间。
// 获取当前时间 2026-09-26T03:12:58.517Z(比当地时间相差8个小时)
System.out.println(Instant.now());
// 获取系统默认时间戳 2026-09-26T11:12:58.517+08:00[Asia/Shanghai]
System.out.println(Instant.now().atZone(ZoneId.systemDefault()));
在 Instant 定义的时间轴上存在三个关键节点:最大点、最小点与原点。以 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 内部采用双字段结构来完整表述时间戳信息。
/**
* The number of seconds from the epoch of 1970-01-01T00:00:00Z.
* 该字段表示Instant时间距离原点1970-01-01T00:00:00Z的时间(单位秒)
*/
private final long seconds;
/**
* The number of nanoseconds, later along the time-line, from the seconds field.
* This is always positive, and never exceeds 999,999,999.
* 该字段表示Instant当前时间的纳秒数这个值不会超过999,999,999,因为1秒=1000_000_000纳秒
*/
private final int nanos;
Instant实例化
常规的 Instant 实例化途径包含以下几种标准方式。
// 获取当前时间
Instant instant1 = Instant.now();
// 字符串转Instant
Instant instant2 = Instant.parse("2026-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;
/**
* @param epochSecond 秒从1970-01-01T00:00:00Z开始计算
* @param nanoAdjustment 纳秒
*/
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小时 2026-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 对象映射于线性时间轴上,因此可以直接执行大小对比操作。
Instant instant1 = Instant.parse("2026-09-26T07:04:19.110Z");
Instant instant2 = Instant.parse("2026-09-26T07:04:19.110Z");
Instant instant3 = Instant.parse("2026-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("2026-09-26T07:04:19.110Z");
// 在instant1的基础上增加2秒,值为:2026-09-26T07:04:21.110Z
System.out.println(instant1.plusSeconds(2));
// 在instant1的基础上增加1毫秒,值为:2026-09-26T07:04:19.111Z
System.out.println(instant1.plusMillis(1));
// 在instant1的基础上增加1001纳秒,值为:2026-09-26T07:04:19.110001001Z
System.out.println(instant1.plusNanos(1001));
// 在instant1的基础上增加1秒,值为:2026-09-26T07:04:20.110Z
// 该值取决于后面指定的单位,可以从ChronoUnit枚举类获取
System.out.println(instant1.plus(1, ChronoUnit.SECONDS));
// 在instant1的基础上减去1秒,值为:2026-09-26T07:04:18.110Z
// plus是增加,minus是减少,逻辑类似可以参考上面plus相关A
System.out.println(instant1.minusSeconds(1));
对 Instant 进行时间点计算时务必注意,无论是调用 plus 还是 minus 相关 API,均会返回并创建一个新的对象实例,原对象保持不变。
至此,关于 Java 时间戳类 Instant 的使用详解已介绍完毕,更多相关 Java 时间戳类 Instant 内容请搜索本站以前的文章或继续浏览下面的相关文章希望大家以后多多支持本站!