【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 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

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()Instantzone 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 互相转换的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程