-
2022-04-25 14:31:39
/* Instant的使用 类似于java.util.Date类 */ @Test public void test2(){ //now():获取本初子午线对应的标准时间 Instant instant = Instant.now(); System.out.println(instant);//2022-04-25T05:56:58.562637400Z //添加时间的偏移量 OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8)); System.out.println(offsetDateTime);//2022-04-25T14:04:52.981486300+08:00 //toEpochMilli():获取自1970年1月1日0时0分0秒(UTC)开始的毫秒数 ---> Date类的getTime() long milli = instant.toEpochMilli(); System.out.println(milli);//1650867450064 //ofEpochMilli():通过给定的毫秒数,获取Instant实例 ---> Date(long millis) Instant instant1 = Instant.ofEpochMilli(1650867450064L); System.out.println(instant1);//2022-04-25T06:17:30.064Z }
更多相关内容 -
Java Instant类
2021-02-27 13:07:17Instant Class : An instantaneous point on the time-...这个类能干什呢?1,处理和时间戳相关的2,但是不处理 年月日这种单位作用1 获得当前时间的毫秒的时间戳Instant timestamp = Instant.now();long ts = times...Instant Class : An instantaneous point on the time-line.
Instant对象表示的就是在时间线上的一点。
Instant对象和时间戳是一一对应的。
这个类能干什呢?
1,处理和时间戳相关的
2,但是不处理 年月日这种单位
作用1 获得当前时间的毫秒的时间戳
Instant timestamp = Instant.now();
long ts = timestamp.toEpochMilli();
既然Instant类是处理时间戳的,肯定可以用ts构造一个Instant对象
ts 转为 Instant对象
long ts2 = 1584700633000L;
Instant instant = Instant.ofEpochMilli(ts2);
LocalDateTime ldt3 = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
System.out.println(ldt3);
Instant类还提供了一些常量,如
EPOCH 是一个常量,也就是 时间戳为0的时刻。代表纪元的开始, 以后的时间戳是正数,之前的时间戳是负数。
MIN:Instant能表示的最早的时间
MAX:Instant能表示的最晚的时间
// 时间移动计算,加1小时, 减5天等
// 一小时以后
Instant timestamp = Instant.now();
Instant oneHourLater = timestamp.plus(1, ChronoUnit.HOURS);
LocalDateTime ldt = LocalDateTime.ofInstant(oneHourLater, ZoneId.systemDefault());
System.out.printf("%s %d %d at %d:%d%n", ldt.getMonth(), ldt.getDayOfMonth(),
ldt.getYear(), ldt.getHour(), ldt.getMinute());
// 两天前
Instant timestamp = Instant.now();
Instant twoDaysAgo = timestamp.minus(2, ChronoUnit.DAYS);
LocalDateTime ldt2 = LocalDateTime.ofInstant(twoDaysAgo, ZoneId.systemDefault());
System.out.printf("%s %d %d at %d:%d%n", ldt2.getMonth(), ldt2.getDayOfMonth(),
ldt2.getYear(), ldt2.getHour(), ldt2.getMinute());
因为Instant类是不处理 年月日这种人类的时间单位,如果要处理,需要把 Instant对象转换为 LocalDateTime 或者 ZonedDateTime 这时需要和一个时区绑定。
Instant timestamp;
LocalDateTime ldt = LocalDateTime.ofInstant(timestamp, ZoneId.systemDefault());
System.out.printf("%s %d %d at %d:%d%n", ldt.getMonth(), ldt.getDayOfMonth(),
ldt.getYear(), ldt.getHour(), ldt.getMinute());
代码:
package com.example.demo;
import static java.time.Instant.EPOCH;
import static java.time.Instant.MAX;
import static java.time.Instant.MIN;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
public class InstantT {
public static void main(String[] args) {
// 获取当前时间戳 Instant timestamp = Instant.now();
long ts = timestamp.toEpochMilli();
// ts 转为 Instant对象 long ts2 = 1584700633000L;
Instant instant = Instant.ofEpochMilli(ts2);
// instant 可以转化为LocalDateTime 表示当地的时间 LocalDateTime ldt3 = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
System.out.println(ldt3);
// LocalDateTime 可以进一步转化为ZonedDateTime,加上了时区的信息 ZonedDateTime zonedDateTime = ZonedDateTime.of(ldt3, ZoneId.systemDefault());
// ZonedDateTime 又可以转换从Instant Instant l = zonedDateTime.toInstant();
System.out.println(l);
boolean equals = instant.equals(l);
System.out.println(equals);
Instant epoch = EPOCH;
System.out.println(epoch);
System.out.println(MIN); //-1000000000-01-01T00:00:00Z System.out.println(MAX); //+1000000000-12-31T23:59:59.999999999Z
// 一小时以后 Instant oneHourLater = timestamp.plus(1, ChronoUnit.HOURS);
LocalDateTime ldt = LocalDateTime.ofInstant(oneHourLater, ZoneId.systemDefault());
System.out.printf("%s %d %d at %d:%d%n", ldt.getMonth(), ldt.getDayOfMonth(),
ldt.getYear(), ldt.getHour(), ldt.getMinute());
// 两天前 Instant twoDaysAgo = timestamp.minus(2, ChronoUnit.DAYS);
LocalDateTime ldt2 = LocalDateTime.ofInstant(twoDaysAgo, ZoneId.systemDefault());
System.out.printf("%s %d %d at %d:%d%n", ldt2.getMonth(), ldt2.getDayOfMonth(),
ldt2.getYear(), ldt2.getHour(), ldt2.getMinute());
}
}
-
Instant类使用
2021-05-27 14:43:42import java.time.Instant; import java.time.OffsetDateTime; import java.time.ZoneOffset;...使用Instant类来获取当前系统时间 并不是当前系统的默认时区 本初子午线 差8小时 东八区 Instant now = Inst.import java.time.Instant; import java.time.OffsetDateTime; import java.time.ZoneOffset; public class InstantTest { public static void main(String[] args) { // 1.使用Instant类来获取当前系统时间 并不是当前系统的默认时区 本初子午线 差8小时 东八区 Instant now = Instant.now(); System.out.println("获取到的当前时间为:" + now); // 2.加上时区所差的8个小时 OffsetDateTime offsetDateTime = now.atOffset(ZoneOffset.ofHours(8)); System.out.println("偏移后的日期时间为:" + offsetDateTime); System.out.println("--------------------------------------------------------"); // 3.获取当前调用对象距离标准基准时间的毫秒数 long g1 = now.toEpochMilli(); System.out.println("获取到的毫秒差为:" + g1); // 4.根据参数指定的毫秒数来构造对象 Instant instant = Instant.ofEpochMilli(g1); System.out.println("根据参数指定的毫秒数构造出来的对象为:" + instant); } }
-
Instant类[java]
2021-11-26 22:03:29Instant类的使用 Instant类的全类名: java.time.Instant; Instant就是瞬时的意思,也就是时间线上的一个瞬时点 Instant可能会被用来记录应用程序中的事件对应的时间戳 我们人都是要看这个时间是几几年多少月...Instant类的使用
Instant类的全类名:
-
java.time.Instant;
-
Instant就是瞬时的意思,也就是时间线上的一个瞬时点
Instant可能会被用来记录应用程序中的事件对应的时间戳
- 我们人都是要看这个时间是几几年多少月多少号,然后看是几时几分,但是电脑不会知道,电脑只会认一个数,这个数可以是秒数,也可以是毫秒数,也就是一个瞬时(Instant)
注意:由于我们的java.time包是基于纳秒计算的,所以我们Instant也可以精确到纳秒级别
实例化:
now();
- 我们使用Instant类来调用其中的now()静态方法就可以得到这个Instant类的实例化对象
- 我们使用now()方法获取的是本初子午线上对应的标准时间
eg:
Instant instant = Instant.now(); //这里就是获取了一个本初子午线上的标准时间
我们这里的输出格式为: 2021-11-26T08:05:02.049066600Z
-
这个时候我们就是获得了一个标准时间(也就是本初子午线上面对应的时间)
-
我们可以通过添加时间的偏移量来获取北京时间
eg:(下面的instant是一个Instant类的实例对象)OffsetDateTime offsetDateTime=instant.atOffset(ZoneOffset.ofHours(8)); //这里我们就是将这个本初子午线对应的标准时间转换成为了北京时间
-
我们这里输出格式为2021-11-26T16:05:02.049066600+08:00
-
成员方法:
-
toEpochMilli();
获取这个瞬时点对应的时间戳(也就是一个毫秒数)
- 这个方法是一个非静态方法,使用Instant类的一个实例化对象调用,将这个Instant类转为long类型的数据
- 从1970年1月1日0时0分0秒到现在的时间差
-
ofEpochMilli();
通过给定的毫秒数的得到一个我们的Instant实例
- 这个方法是一个静态方法,调用这个ofEpochMill()方法传入一个long型的数据,就可以转换成为Instant类的对象
-
-
详解Instant类
2020-04-30 12:18:111.Instant类类似于java.util.Date类 2. public static void main(String[] args){ //获取本初子午线标准时间 Instant instant=Instant.now(); System.out.println(instant);//与东八区时间相差8小时 //添加时间... -
Java基础~Java Instant类
2022-03-07 17:32:36Instant类由一个静态的工厂方法now()可以返回当前时间戳 时间戳是包含日期和时间的,与java.util.Date很类似,事实上Instant就是类似JDK8以前的Date Instant和Date这两个类可以进行转换 二、实例 public static ... -
JDK8 Instant类的使用和LocalDate,LocalTime 和 LocalDateTime的介绍。
2020-07-27 17:10:40本文章主要介绍的就是Instant,LocalDate,LocalTime 和 LocalDateTime这几个类。 UTC 世界标准时间“Coordinated Universal Time”,因为地区的差异,所以有时差。比如: 北京的白天,纽约却是夜晚; 两个人... -
[Java] Instant类介绍与应用
2020-06-27 09:51:13Instant是JKD1.8中新增的日期API,简要说下Java中日期APi的背景。 早在JDK1.0时代,JDK就包含了一个与时间相关的java.util.Date类,而因为其本身的缺陷,其大多数方法都在JDK1.1时代被新引入的与时间相关的Calendar... -
计算机后端-Java-Java核心基础-第22章 常用类 16. Instant类的使用.avi
2022-05-22 16:05:20计算机后端-Java-Java核心基础-第22章 常用类 16. Instant类的使用.avi -
Day40.Instant类、DateTimeFormatter类与传统日期处理的转换 -Java常用类#、集合、IO
2020-09-13 18:46:21Instant类 Instant: 时间线上的一个瞬时点。 这可能被用来记录应用程序中的事件时间戳 在处理时间和日期的时候,我们通常会想到年,月,日,时,分,秒。 然而,这只是时间的一个模型,是面向人类的。第二种通用... -
Instant类的使用
2021-09-25 22:48:56 -
java高级 --- instant类
2021-01-05 10:04:03Instant类 类似于java.utils.Date //now获取的是本初子午线的时间 Instant now = Instant.now(); System.out.println(now); //2020-09-11T13:32:36.046Z 本初子午线时间 //添加时间的偏移量 OffsetDateTime ... -
Instant类--Java
2021-02-01 16:20:56Instant now = Instant.now(); System.out.println(now); //设置东八区偏移量 8小时 OffsetDateTime offsetDateTime = now.atOffset(ZoneOffset.ofHours(8)); System.out.println(offsetDateTime); -
Java8 新特性 - Instant 时间戳类
2022-03-27 10:03:31Instant 类 是Java8 中补充的一个 时间戳类。 相较于 System.currentTimeMillis()获取到【毫秒】,Instant 可以更为精确的获取到【纳秒】。 Instant 可以使用静态方法 now() 或者 of() 方法来创建一个实例对象。... -
Java学习 时间类 Period类与Duration类 / LocalDate类与Instant类 用法详解
2021-03-11 15:48:40前言java 8 中引入的两个与日期相关的新类:Period 和 Duration。两个类看表示时间量或两个日期之间的差,两者之间的差异为:Period基于日期值,而Duration基于时间值。他们估计最大的作用就不需要你自己复杂的计算... -
Java常用类(2)--日期时间相关类Date、Calendar、LocalDateTime、Instant全面
2021-09-08 18:33:29文章目录java.lang.System类java.util.Date类java.sql.Date类java.text.SimpleDateFormat类java.util.Calendar(日历)类 java.lang.System类 System类提供的public static long currentTimeMillis()用来返回当前... -
Instant
2021-03-09 21:04:06我们已经讲过,计算机存储的当前时间,本质上只是一...这个当前时间戳在java.time中以Instant类型表示,我们用Instant.now()获取当前时间戳,效果和System.currentTimeMillis()类似:import java.time.*;----public ... -
Instant 时间戳类
2019-05-30 11:35:20从1970-01-01 00:00:00 截止到当前时间的毫秒值 1获取对象的方法 now() ... Instant ins = Instant.now(); System.out.println(ins); 我们在东八区 所以可以加8个小时 就是我们的北京时间 2. Instant中设置偏移...