Java 8 Instant 时间戳小记

Java 8 Instant 时间戳小记Java8Instant 时间戳 1 创建 Instant 实例 获取系统的当前时间 now Java8Instant 时间戳学习 Testpublicvo 通过 Instant 创建 Instant 实例返回 returnClock systemUTC instant Instantnow Instant now 控制台输出

Java 8 Instant 时间戳

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

 / * Java 8 Instant时间戳学习 */ @Test public void testInstant(){ 
    // 通过Instant创建Instant实例 返回:return Clock.systemUTC().instant(); Instant now = Instant.now(); //控制台输出:now = 2020-12-29T06:32:49.480Z (以ISO-8601格式输出) System.out.println("now = " + now); } 

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

Intance的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); 

输出结果

2020-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 = 2020-12-29T14:35:32.631Z

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

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

  1. 通过 getEpochSecond()方法获取距离格林威治时间的秒数
  2. 通过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() + "毫秒"); 

输出结果:

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("2020-12-29T14:35:32.631Z")); 

输出结果

2020-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)); //返回 

输出结果:

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); 
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/199825.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月20日 下午12:07
下一篇 2026年3月20日 下午12:07


相关推荐

  • struts2注解配置action_java struts框架

    struts2注解配置action_java struts框架 ActionContext是Action的上下文,Struts2自动在其中保存了一些在Action执行过程中所需的对象,比如session,parameters,locale等。Struts2会根据每个执行HTTP请求的线程来创建对应的ActionContext,即一个线程有一个唯一的ActionContext。因此,使用者可以使用静态方法ActionContext.getContext(…

    2025年10月14日
    4
  • linux 文件夹修改名字,linux命令修改文件名_修改文件夹名字怎么操作

    linux 文件夹修改名字,linux命令修改文件名_修改文件夹名字怎么操作如何用命令修改文件名呢 在 Linux 下可以用 rename 命令 当然还可以使用 mv 命令如下 Linux 修改文件名语句 mvoldFileNam 示例 我想把 aaa txt 修改为 bbb txt 示例语句 mvaaa txtbbb txt 把当前目录下的 file1 文件名改成 file2 如果该目录下有 file2 则覆盖以前的 file2 文件

    2026年3月20日
    2
  • arm汇编传参

    arm汇编传参开发环境 androidstudi 3 调试工具 IDA7 0so 代码 include lt jni h gt include lt string gt include lt stdlib h gt include lt stdio h gt intGetSum inta1 inta2 intsum a1 a2 re

    2026年3月18日
    3
  • alternatives命令总结

    alternatives命令总结基本用法 用 alternatives 查看目前系统中有哪些 java 版本 alternatives 注册新版 java sudoalternat usr bin javajava usr local java jre1 8 0 131 bin java4 配置有效 java sudoalternat

    2026年3月19日
    2
  • h5网页制作_为什么叫h5页面

    h5网页制作_为什么叫h5页面背景:h5文件详解H5文件是层次数据格式第5代的版本(HierarchicalDataFormat,HDF5),它是用于存储科学数据的一种文件格式和库文件。由美国超级计算中心与应用中心研发的文件格式,用以存储和组织大规模数据.H5将文件结构简化成两个主要的对象类型: 数据集dataset,就是同一类型数据的多维数组 组group,是一种容器结构,可以包含数据集和其他组,若一个文件中存放了不同种类的数据集,这些数据集的管理就用到了group! 直观的理解,可以参考我们的文件系统

    2025年10月13日
    6
  • win10 pycharm无法打开 双击无反应

    win10 pycharm无法打开 双击无反应pycharm 打不开 重装无效 netshwinsock 重置一下网络状态一个命令解决 怀疑是前两天做美赛安装 latex 导致系统环境出现问题 查这个问题的人还挺多的 更新一下 还有可能是电脑配置比较低 pycharm 在初始化中 如果进程中能看到的话

    2026年3月27日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号