JAVA8 Instant时间戳 使用案例

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

二、文档介绍与实例
1.now
JAVA8 Instant时间戳 使用案例_第1张图片

/*
 * NOW
 */
Instant now = Instant.now();
System.out.println("now:" + now);
ZonedDateTime zonedDateTime = now.atZone(ZoneId.systemDefault());//获取当前时区时间
System.out.println("当前时区时间:" + zonedDateTime);
long shi = zonedDateTime.toEpochSecond(); //获取十位时间戳
System.out.println("当前时区时间的时间戳:" + shi);

结果:
在这里插入图片描述
注意:通过这种方式获取的时间戳与北京时间相差8个时区,需要修正为北京时间,通过查看源代码发现Instant.now()使用等是UTC时间Clock.systemUTC().instant()。

2.ofEpochSecond
JAVA8 Instant时间戳 使用案例_第2张图片

/*
* ofEpochSecond
 */
Instant instant3 = Instant.ofEpochSecond(3);
Instant instant05 = Instant.ofEpochSecond(0, 5);
System.out.println(instant3);
System.out.println(instant05);

结果为:
在这里插入图片描述

3.ofEpochMilli
JAVA8 Instant时间戳 使用案例_第3张图片

Instant ofEpochMilli = Instant.ofEpochMilli(500000);
System.out.println(ofEpochMilli);

在这里插入图片描述

4.from
JAVA8 Instant时间戳 使用案例_第4张图片

5.parse
JAVA8 Instant时间戳 使用案例_第5张图片

持续更新

你可能感兴趣的:(java,JAVA8,JDK8,时间)