本文将提供如何在 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
之间转换的示例。
查找将 LocalDateTime
转换为 Instant
的示例。
LocalDateTimeToInstant.java
package com.concretepage;
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
LocalDateTime.toInstant()
将此日期时间转换为 Instant
。找到 Java 文档。
Instant toInstant(ZoneOffset offset)
找到代码片段以使用它。
Instant instant = localDateTime.toInstant(ZoneOffset.UTC);
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);
查找将 Instant
转换为 LocalDateTime
的示例。
InstantToLocalDateTime.java
package com.concretepage;
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
LocalDateTime.ofInstant()
从 Instant
和区域 ID 获取 LocalDateTime
的实例。找到 Java 文档。
static LocalDateTime ofInstant(Instant instant, ZoneId zone)
找到代码片段以使用它。
LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.now(), ZoneId.systemDefault());
Timestamp.from()
从 Instant
对象获取 Timestamp
的实例。找到 Java 文档。
static Timestamp from(Instant instant)
然后使用 LocalDateTime.toLocalDateTime()
获取 LocalDateTime
实例。找到代码片段。
localDateTime = Timestamp.from(Instant.now()).toLocalDateTime();
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://www.concretepage.com/java/java-8/convert-between-java-localdatetime-instant
内容来源于网络,如有侵权,请联系作者删除!