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

本页将提供如何在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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猫巳

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值