当前位置: 首页 > 编程文章 >

Java8 Instant时间戳使用指南

Java8 Instant时间戳使用指南

作者:顾景时间:2026-06-30 15:03:17浏览:816

Java 8引入了java.time.Instant类用于处理时间戳,其基于Unix元年(1970年1月1日午夜)的秒数进行运算。Instant默认表示世界标准时间(UTC),即0时区。本文介绍了创建Instant实例、转换为本地时间(如北京时间)、获取距离格林威治时间的秒/毫秒值,以及使用from和parse方法解析时间等常用操作。

本文目录收起

Java 8 Instant 时间戳

主要用于“时间戳”相关的运算。它以Unix元年(传统设定为UTC时区1970年1月1日午夜时分)开始所经历的时间描述进行计算。

1. 创建Instant实例,获取系统的当前时间now


 /**
  * Java 8 Instant时间戳学习
  */
 @Test
 public void testInstant(){
  // 通过Instant创建Instant实例 返回:return Clock.systemUTC().instant();
  Instant now = Instant.now();

  //控制台输出:now = 2026-12-29T06:32:49.480Z (以ISO-8601格式输出)
  System.out.println("now = " + now);
 }

注意:这里控制台输出:now = 2026-12-29T06:32:49.480Z。

Instant的now方法:


 public static Instant now() {
  return Clock.systemUTC().instant();
 }

这是输出的世界标准时间,其中T表示时分秒的开始(或者日期与时间的间隔),Z表示这是一个世界标准时间。

Instant 是时间戳,是指世界标准时格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数,Instant本身实际上是指明时区了,是0时区(也就是比北京时间少8小时)。

2. 获取当前时区的时间(本地时间)

2.1 通过方法Instant.now().atZone(ZoneId.systemDefault())获取当前地区的时间


  ZonedDateTime zonedDateTime = Instant.now().atZone(ZoneId.systemDefault());
  System.out.println(zonedDateTime);

输出结果

2026-12-31T17:31:14.953+08:00[Asia/Shanghai]

2.2 通过增加8小时,转化为北京时间

方法名称 描述
plusMillis() 增加时间戳时间,以毫秒为单位
minusNanos() 增加时间戳时间,以纳秒为单位
minusSeconds() 增加时间戳时间,以秒为单位
TimeUnit.HOURS.toMillis() 将小时转化为毫秒数

  //增加8个小时,使Instant.now()返回时间为北京时间
  Instant now2 = Instant.now().plusMillis(TimeUnit.HOURS.toMillis(8));
  System.out.println("now2 = " + now2);

输出结果:now2 = 2026-12-29T14:35:32.631Z

转换为符合当前的北京时间。

3. 通过Instant获取当前时间距离格林威治时间的值

通过 getEpochSecond()方法获取距离格林威治时间的秒数

通过toEpochMilli()方法获取距离格林威治时间的毫秒数


  //增加8个小时,使Instant.now()返回时间为北京时间
  Instant now2 = Instant.now().plusMillis(TimeUnit.HOURS.toMillis(8));
  //获取格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)距离当前时间的秒/毫秒值
  System.out.println("距离1970年01月01日00时00分00秒 : "+now2.getEpochSecond() + "秒");
  System.out.println("距离1970年01月01日00时00分00秒 : "+now2.toEpochMilli() + "毫秒");

输出结果:

距离1970年01月01日00时00分00秒 : 1609435201秒 距离1970年01月01日00时00分00秒 : 1609435202645毫秒

4. Instant的from、parse方法

4.1 java.time.Instant.from(TemporalAccessor temporal)源码:


 public static Instant from(TemporalAccessor temporal) {
  if (temporal instanceof Instant) {
   return (Instant) temporal;
  }
  Objects.requireNonNull(temporal, "temporal");
  try {
   long instantSecs = temporal.getLong(INSTANT_SECONDS);
   int nanoOfSecond = temporal.get(NANO_OF_SECOND);
   return Instant.ofEpochSecond(instantSecs, nanoOfSecond);
  } catch (DateTimeException ex) {
   throw new DateTimeException("Unable to obtain Instant from TemporalAccessor: " +
     temporal + " of type " + temporal.getClass().getName(), ex);
  }
 }

参数:temporal 是要转换的时间对象,返回的是一个转换为Instant的瞬间值

如果转换为Instant的时候失败,会抛出异常``DateTimeException`

4.2 parse方法源码


 public static Instant parse(final CharSequence text) {
  return DateTimeFormatter.ISO_INSTANT.parse(text, Instant::from);
 }

创建自定义的时间戳


  //创建自定义的时间戳
  System.out.println(Instant.parse("2026-12-29T14:35:32.631Z"));	

输出结果

2026-12-29T14:35:32.631Z

5. Instant的其它常用函数


//获取当前时间戳
  Instant instant = Instant.now();
  //获得当前时间戳并且增加66毫秒
  Instant instant1 = Instant.now().plusMillis(66);
  //获得当前时间戳并且减少66毫秒
  Instant instant2 = Instant.now().minusMillis(66);

  //判断时间戳 instant 是否在 instant1 之后,返回boolean
  System.out.println(instant.isAfter(instant1)); //返回false
  //判断时间戳 instant 是否在 instant1 之前,返回boolean
  System.out.println(instant.isBefore(instant1)); //返回true
  //判断两个时间戳是否相等, 返回boolean值
  System.out.println(instant.equals(instant1)); //返回false



  //获得当前时间戳并增加1小时 通过TimeUnit.HOURS.toMillis(1)将小时转换为毫秒,然后通过plusMillis增加
  Instant instant3 = Instant.now().plusMillis(TimeUnit.HOURS.toMillis(1));
  //获取时间戳 instant和instant3 相差天数,返回long类型
  //如果小于1天,都算零天,大于等于1天,小于2天算一天
  System.out.println("相差天数 = " + instant.until(instant3, ChronoUnit.DAYS)); //返回0
  //获取时间戳 instant和instant3 相差的小时数,返回long类型
  System.out.println("相差小时 = " + instant.until(instant3, ChronoUnit.HOURS)); //返回1
  //获取时间戳 instant和instant3 相差的毫秒数,返回long类型
  System.out.println("相差毫秒数 = " + instant.until(instant3, ChronoUnit.MILLIS)); //返回3600000

输出结果:

false true false 相差天数 = 0 相差小时 = 1 相差毫秒数 = 3600000

6. 将获取的时间戳转化为LocalDate


 Instant now = Instant.now();
 //UTC
 ZonedDateTime atZone = now.atZone(ZoneOffset.UTC);
 	//LocalDateTime
 atZone.toLocalDateTime();
 LocalDateTime.from(atZone);

 //LocalDate
 atZone.toLocalDate();
 LocalDate date = LocalDate.from(atZone);
 //LocalDateTime
 atZone.toLocalDateTime();
 LocalDateTime.from(date);

总结

到此这篇关于Java8 Instant时间戳使用小记的文章就介绍到这了,更多相关Java8 Instant时间戳内容请搜索本站以前的文章或继续浏览下面的相关文章希望大家以后多多支持本站!

上一篇
Java 8 Instant 时间戳类详解:从毫秒到纳秒的精度升级

JDK 1.8 推出的 Instant 类解决了 Date 类方法废弃及毫秒精度不足的问题,支持纳秒级时间表示。该类基于 UTC 时间线,通过 seconds 和 nanos 两个字段记录时间点。本文详解 Instant 的多种实例化方式、参数获取、时间比较及不可变对象的加减运算特性。

下一篇
Java 8 Instant 时间获取、时区修正与转换详解

Java 8 中的 Instant.now() 默认返回 UTC 时间,与北京时间存在 8 小时时差。本文介绍如何通过 plusMillis 方法增加 8 小时以修正为北京时间,并演示 Instant 获取秒级和毫秒级时间戳的方法。同时对比 LocalDateTime 转换为 Instant 的过程,以及 Date 对象转换为指定格式字符串的工具代码实现。

相关文章

更多
Java8 Collectors.toMap 默认抛出 Duplicate Key 异常的原因与解决

在使用 Java8 Stream API 的 Collectors.toMap 方法时,若流中存在重复键,默认行为并非覆盖旧值而是抛出 Duplicate KeyException。本文分析其底层通过 throwingMerger 实现机制,并演示如何通过传入自定义 mergeFunction 参数来指定新值替换旧值的处理逻辑,避免线上业务异常。

2026.08.26 963
Java8 StringJoiner:优雅实现字符串拼接

Java8引入了StringJoiner类,旨在简化并美化字符串拼接操作。相比传统的StringBuilder或手动处理分隔符,StringJoiner通过构造函数直接指定分隔符及前后缀,显著提升了代码的可读性与简洁性。本文通过对比不同拼接方式,展示StringJoiner在列表转字符串、合并集合及空值处理等场景下的具体用法。

2025.09.21 935
Java 8与Scala Lambda表达式对比解析

Java 8正式引入Lambda表达式,旨在简化回调代码并支持函数式编程。本文深入解析Java 8中Lambda的语法结构、类型推论机制及其作为闭包的特性,并通过具体示例展示其如何替代匿名内部类。同时,文章对比了Scala中的函数文本(Function Literal),指出两者在语义上的高度一致性及箭头符号等语法差异,帮助开发者理解跨语言函数式编程的核心概念。

2026.09.08 676
Java 8 Instant 时间获取、时区修正与转换详解

Java 8 中的 Instant.now() 默认返回 UTC 时间,与北京时间存在 8 小时时差。本文介绍如何通过 plusMillis 方法增加 8 小时以修正为北京时间,并演示 Instant 获取秒级和毫秒级时间戳的方法。同时对比 LocalDateTime 转换为 Instant 的过程,以及 Date 对象转换为指定格式字符串的工具代码实现。

2025.10.06 622
Java8 Instant时间戳使用详解与时区处理

Instant是Java8新增的时间戳类,支持纳秒级精度。通过now()或of()创建实例后,可利用getEpochSecond()和toEpochMilli()分别获取10位秒数和13位毫秒数。需注意Instant.now()默认返回UTC时间,与北京时间存在8小时时差,需通过plusMillis增加8小时进行修正。该类还可作为java.util.Date与LocalDateTime转换的桥梁。

2025.10.07 442
Java 8 Instant 时间戳类详解:从毫秒到纳秒的精度升级

JDK 1.8 推出的 Instant 类解决了 Date 类方法废弃及毫秒精度不足的问题,支持纳秒级时间表示。该类基于 UTC 时间线,通过 seconds 和 nanos 两个字段记录时间点。本文详解 Instant 的多种实例化方式、参数获取、时间比较及不可变对象的加减运算特性。

2026.04.23 165

专题推荐

更多
PHP 类与对象定义及继承

先看类声明、对象实例与属性访问。理解对象的创建和销毁后,再读继承、方法重载和关键字对照。已有代码遇到继承行为差异时,可进一步查看 final 方法与反射实例。

PHP 定时任务配置与运行

先确定任务运行间隔以及是否允许重叠。固定时间触发先看 Cron 案例,框架内和常驻服务再读对应示例。

Yii2 队列配置与常驻运行

先用一个最小任务跑通入队和消费,再检查 Web 与 Console 是否加载了相同的队列配置。确认命令能手动运行后再配置 Supervisor,最后验证进程重启时的处理情况。

PHP Session 过期时间设置方法

浏览器关闭、服务端回收和业务规定的闲置超时,可能在不同时间发生。对照配置项与应用层时间戳的做法,检查会话何时仍可读、何时应要求重新登录。

PHP URL 编码和解码方法

URL 路径片段和查询参数对空格的处理方式不同。先比较 urlencode 与 rawurlencode,再沿解码过程核对中文、加号和百分号,检查地址是否被重复编码或提前解码。

在线工具箱

更多
键码查询
键码查询

按下键盘按键时查看浏览器记录的键名和事件信息。

.env 配置检查
.env 配置检查

本地检查 .env 键值语法、引号、重复键及空值,预览解析结果,辅助排查 PHP 和 Laravel 环境配置。

进制转换
进制转换

转换二进制、十进制、十六进制等数值表示,辅助程序调试与进制学习。

HTML 标签安全清理
HTML 标签安全清理

清理 HTML 片段中的标签或危险内容,辅助整理待展示的文本。

文件 Base64 转换
文件 Base64 转换

把文件转为 Base64 文本,也可将编码内容还原为文件,方便接口传输与调试。