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

标签:java8,instant,时间戳
0
投稿

猜你喜欢

  • Android 使用 Scroller 实现平滑滚动功能的示例代码

    2022-01-20 22:49:35
  • MybatisPlus如何自定义TypeHandler映射JSON类型为List

    2023-08-08 14:05:38
  • SpringBoot MainApplication类文件的位置详解

    2023-10-28 16:21:12
  • 学习Android自定义Spinner适配器

    2022-12-15 01:28:31
  • java web开发之购物车功能实现示例代码

    2023-01-24 16:52:55
  • 如何在XML中定义菜单

    2023-08-08 11:49:33
  • Java实现一个简单的定时器代码解析

    2021-11-24 20:25:38
  • C#双缓冲技术实例详解

    2023-02-02 15:56:37
  • 深入谈谈C#9新特性的实际运用

    2021-05-26 16:08:23
  • Java中equalsIgnoreCase()方法的使用

    2022-07-28 15:13:45
  • 举例讲解C#编程中对设计模式中的单例模式的运用

    2023-04-28 19:34:10
  • JavaWeb中的常用的请求传参注解说明

    2023-06-19 03:12:06
  • MyBatis通用的10种写法总结大全

    2022-08-01 12:04:02
  • Android setTag方法的key问题解决办法

    2021-09-12 14:29:37
  • SpringBoot服务开启后通过端口访问无反应的解决

    2022-12-11 03:57:17
  • Java HelloWorld原理分析_动力节点Java学院整理

    2023-05-23 21:34:14
  • C#实现一键换IP、重置DNS、网关及掩码的方法

    2021-10-03 00:32:33
  • 通过实例深入了解java序列化

    2022-12-01 17:37:39
  • C#访问及调用类中私有成员与方法示例代码

    2022-06-02 05:30:35
  • springboot如何获取相对路径文件夹下静态资源的方法

    2023-07-12 06:23:09
  • asp之家 软件编程 m.aspxhome.com