ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2021-09-07 17:02:27  阅读:530  来源: 互联网

标签:Instant instant ofInstant localDateTime Java LocalDateTime


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

标签:Instant,instant,ofInstant,localDateTime,Java,LocalDateTime
来源: https://blog.csdn.net/qq_31635851/article/details/120160300

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有