【Java 8 新特性】Java LocalDateTime 和 Instant 互相转换
2021/9/7 17:06:16
本文主要是介绍【Java 8 新特性】Java LocalDateTime 和 Instant 互相转换,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
Java LocalDateTime 和 Instant 互相转换
- 1. LocalDateTime 转 Instant
- 1.1 使用 LocalDateTime.toInstant() 将LocalDateTime 转化为 Instant
- 1.2 使用 LocalDateTime.toEpochSecond() 和 Instant.ofEpochSecond() 将LocalDateTime 转化为 Instant
- 2. Instant 转 LocalDateTime
- 2.1 使用 LocalDateTime.ofInstant() 将 Instant 转化为 LocalDateTime
- 2.1 使用 Timestamp.from() 将 Instant 转化为 LocalDateTime
本页将提供如何在Java LocalDateTime
和Instant
之间转换。
LocalDateTime
表示没有时区的日期时间,如2019-10-25T12:15:30
,而Instant
是时间线上的一个瞬时点。
我们可以通过以下方式在Java LocalDateTime
和Instant
之间进行转换。
1. 使用LocalDateTime.toInstant()
方法将LocalDateTime
转换为Instant
。
Instant instant = localDateTime.toInstant(ZoneOffset.UTC);
2. 使用LocalDateTime.ofInstant()
方法将Instant
转换成LocalDateTime
。
LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.now(), ZoneId.systemDefault());
现在找到在Java LocalDateTime
和Instant
之间转换的详细例子。
1. LocalDateTime 转 Instant
查找将LocalDateTime
转换为Instant
的示例。
LocalDateTimeToInstant.java
import java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZoneOffset; public class LocalDateTimeToInstant { public static void main(String[] args) { LocalDateTime localDateTime = LocalDateTime.parse("2019-10-25T12:15:30"); //Using LocalDateTime.toInstant() Instant instant = localDateTime.toInstant(ZoneOffset.UTC); System.out.println(instant); instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant(); System.out.println(instant); //Using LocalDateTime.toEpochSecond() and Instant.ofEpochSecond() long timeInSeconds = localDateTime.toEpochSecond(ZoneOffset.UTC); instant = Instant.ofEpochSecond(timeInSeconds); System.out.println(instant); } }
输出
2019-10-25T12:15:30Z 2019-10-25T06:45:30Z 2019-10-25T12:15:30Z
1.1 使用 LocalDateTime.toInstant() 将LocalDateTime 转化为 Instant
LocalDateTime.toInstant()
将这个日期时间转换为Instant
。
查找Java
文档。
Instant toInstant(ZoneOffset offset)
使用示例
Instant instant = localDateTime.toInstant(ZoneOffset.UTC);
1.2 使用 LocalDateTime.toEpochSecond() 和 Instant.ofEpochSecond() 将LocalDateTime 转化为 Instant
LocalDateTime.toEpochSecond()
将这个日期时间转换为从1970-01-01T00:00:00Z
这个纪元开始的秒数。
查找Java
文档。
long toEpochSecond(ZoneOffset offset)
Instant.ofEpochSecond()
使用1970-01-01T00:00:00Z
这个纪元的秒数来获得一个Instant
的实例。
查找Java
文档。
static Instant ofEpochSecond(long epochSecond)
我们可以使用LocalDateTime.toEpochSecond()
和Instant.ofEpochSecond()
来将LocalDateTime
转换为Instant
,方法如下。
long timeInSeconds = localDateTime.toEpochSecond(ZoneOffset.UTC); instant = Instant.ofEpochSecond(timeInSeconds);
2. Instant 转 LocalDateTime
将Instant
转换为LocalDateTime
的示例。
InstantToLocalDateTime.java
import java.sql.Timestamp; import java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneId; public class InstantToLocalDateTime { public static void main(String[] args) { //Using LocalDateTime.ofInstant LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.now(), ZoneId.systemDefault()); System.out.println(localDateTime); long timeInSeconds = 1567109422L; localDateTime = LocalDateTime.ofInstant(Instant.ofEpochSecond(timeInSeconds), ZoneId.systemDefault()); System.out.println(localDateTime); localDateTime = LocalDateTime.ofInstant(Instant.ofEpochSecond(timeInSeconds, 0), ZoneId.systemDefault()); System.out.println(localDateTime); long timeInMillis = 1567109422123L; localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(timeInMillis), ZoneId.systemDefault()); System.out.println(localDateTime); //Using Timestamp localDateTime = Timestamp.from(Instant.now()).toLocalDateTime(); System.out.println(localDateTime); } }
输出
2019-09-03T09:17:47.749482700 2019-08-30T01:40:22 2019-08-30T01:40:22 2019-08-30T01:40:22.123 2019-09-03T09:17:47.828487200
2.1 使用 LocalDateTime.ofInstant() 将 Instant 转化为 LocalDateTime
LocalDateTime.ofInstant()
从Instant
和zone ID
获得LocalDateTime
的一个实例。
查找Java
文档。
static LocalDateTime ofInstant(Instant instant, ZoneId zone)
使用示例
LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.now(), ZoneId.systemDefault());
2.1 使用 Timestamp.from() 将 Instant 转化为 LocalDateTime
Timestamp.from()
从Instant
对象中获得一个Timestamp
的实例。
查找Java
文档。
static Timestamp from(Instant instant)
使用示例
localDateTime = Timestamp.from(Instant.now()).toLocalDateTime();
【1】Class LocalDateTime
【2】Class Instant
【3】Convert between Java LocalDateTime and Instant
这篇关于【Java 8 新特性】Java LocalDateTime 和 Instant 互相转换的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-15鸿蒙生态设备数量超8亿台
- 2024-05-13TiDB + ES:转转业财系统亿级数据存储优化实践
- 2024-05-09“2024鸿蒙零基础快速实战-仿抖音App开发(ArkTS版)”实战课程已上线
- 2024-05-09聊聊如何通过arthas-tunnel-server来远程管理所有需要arthas监控的应用
- 2024-05-09log4j2这么配就对了
- 2024-05-09nginx修改Content-Type
- 2024-05-09Redis多数据源,看这篇就够了
- 2024-05-09Google Chrome驱动程序 124.0.6367.62(正式版本)去哪下载?
- 2024-05-09有没有大佬知道这种数据应该怎么抓取呀?
- 2024-05-09这种运行结果里的10.100000001,怎么能最快改成10.1?