Java中LocalDateTime与 Instant 之间的转换

x33g5p2x  于2022-09-24 转载在 Java  
字(3.6k)|赞(0)|评价(0)|浏览(295)

本文将提供如何在 Java LocalDateTimeInstant 之间进行转换。 LocalDateTime 表示没有时区的日期时间,例如 2019-10-25T12:15:30,而 Instant 是时间线上的瞬时点。我们可以通过以下方式在 Java LocalDateTimeInstant 之间进行转换。
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 LocalDateTimeInstant 之间转换的示例。

1. 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

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

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

2.1 使用 LocalDateTime.ofInstant() 将Instant 转换为 LocalDateTime

LocalDateTime.ofInstant()Instant 和区域 ID 获取 LocalDateTime 的实例。找到 Java 文档。

static LocalDateTime ofInstant(Instant instant, ZoneId zone)

找到代码片段以使用它。

LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.now(), ZoneId.systemDefault());

2.2 使用 Timestamp.from() 将Instant 转换为 LocalDateTime

Timestamp.from()Instant 对象获取 Timestamp 的实例。找到 Java 文档。

static Timestamp from(Instant instant)

然后使用 LocalDateTime.toLocalDateTime() 获取 LocalDateTime 实例。找到代码片段。

localDateTime = Timestamp.from(Instant.now()).toLocalDateTime();

相关文章