【Java 8 新特性】Java LocalDateTime 和 Instant 互相转换

您所在的位置:网站首页 燕的词语成语 【Java 8 新特性】Java LocalDateTime 和 Instant 互相转换

【Java 8 新特性】Java LocalDateTime 和 Instant 互相转换

2023-10-12 04:11| 来源: 网络整理| 查看: 265

Java LocalDateTime 和 Instant 互相转换 1. LocalDateTime 转 Instant1.1 使用 LocalDateTime.toInstant() 将LocalDateTime 转化为 Instant1.2 使用 LocalDateTime.toEpochSecond() 和 Instant.ofEpochSecond() 将LocalDateTime 转化为 Instant 2. Instant 转 LocalDateTime2.1 使用 LocalDateTime.ofInstant() 将 Instant 转化为 LocalDateTime2.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



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3