Java8 Instant时间戳使用小记
作者:strive_day 发布时间:2023-05-31 10:33:59
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 = 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获取当前时间距离格林威治时间的值
通过 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秒 : 1609435201645毫秒
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)); //返回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);
总结
来源:https://blog.csdn.net/qq_40542534/article/details/112043198


猜你喜欢
- 入住博客园4年多了,一直都是看别人的博客,学习别人的知识,为各个默默无私贡献自己技术总结的朋友们顶一个;这几天突然觉得是时候加入该队列中,贡
- 一、添加联系人Intent addIntent = new Intent(Intent.ACTION_INSERT,Uri.withAppe
- 简介FTP是TCP/IP协议组中的协议之一,包括两个组成部分,一是FTP服务端,二是FTP客户端,其中FTP服务器用来存储文件,用户可以使用
- 1.由于springboot集成了tomcat,所以打包的时候不再使用war,而是使用jar <groupId>cn&
- 前言我们从以下几个方面研究:SpringBoot的启动依赖启动器starter有什么作用启动引导类是怎么运行的内置的tomcat服务器原理p
- 步骤:1、创建一个项目,该项目主要用来设计用户控件。2、创建一个用户控件窗体,用来设计用户控件。3、向用户控件窗体中添加一个按钮(butto
- 在spring cloud系列章节中,本来已经写了几个章节了,但是自己看起来有些东西写得比较杂,所以重构了一下springcloud的章节内
- 1、java 的下载和安装一、安装JDKjava下载网址 或者 点击这里根据自己操作系统和系统位数下载相应的JDK安装
- 在前台请求数据的时候,sql语句一直都是打印到控制台的,有一个想法就是想让它打印到日志里,该如何做呢?见下面的mybatis配置文件:<
- 本节我们开始自我实现我们自己okhttp框架中的每个 * 。先简单回顾一下各个 * 的作用:RetryAndFollowUpIntercep
- groovy是一种动态脚本语言,适用于一些可变、和规则配置性的需求,目前Spring提供ScriptSource接口,支持两种类型,一种是R
- 1、先看一下项目目录:2、新建一个AS项目,创建如上图所示的目录结构,然后添加内容:(1)修改添加布局文件:activity_main.xm
- 数据绑定API用于JSON转换和使用属性访问或使用注解POJO(普通Java对象)。以下是它的两个类型。简单数据绑定 - 转换JSON,从J
- 本文实例讲述了Android开发使用Handler的PostDelayed方法实现图片轮播功能。分享给大家供大家参考,具体如下:第一步:创建
- route_generator是什么这是一个简单的 Flutter 路由生成库,只需要少量的代码,然后利用注解配合源代码生成,自动生成路由表
- springboot扩展MVC自定义 config -> SpringMvcConfig.java下边就是扩展springMVC的模板
- byte:java中最小的数据类型。1字节/8位。-128(2^7)~127(2^7-1),默认值0。short:短整型,2字节/16位,取
- 一、准备工作小编今天以 QQ邮箱 进行演示操作。想要使用代码操作邮箱发送邮件,需要在邮箱设置中申请开通 POP3/SMTP 服务。接下来跟着
- 今天来说一个Java多机部署下定时任务的处理方案。需求: 有两台服务器同时部署了同一套代码, 代码中写有spring自带的定时任务,但是每次
- 现在Android上架各大平台都要求App首页添加一个弹框,显示用户协议以及一些隐私政策,不然上架各大平台,现在就来简单的实现一下这个对话框