Java 8 examples to show you how to convert from Instant to LocalDateTime

1. Instant -> LocalDateTime

The java.time.LocalDateTime has no concept of time zone, just provide a zero offset UTC+0.

InstantExample1.java
package com.mkyong.date;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset; public class InstantExample1 { public static void main(String[] argv) { // Parse a ISO 8601 Date directly
//Instant instant = Instant.parse("2016-08-18T06:17:10.225Z"); Instant instant = Instant.now(); System.out.println("Instant : " + instant); //Convert instant to LocalDateTime, no timezone, add a zero offset / UTC+0
LocalDateTime ldt = LocalDateTime.ofInstant(instant, ZoneOffset.UTC); System.out.println("LocalDateTime : " + ldt); } }
 

Output

Instant : 2016-08-18T06:17:10.225Z
LocalDateTime : 2016-08-18T06:17:10.225

2. LocalDateTime -> Instant

InstantExample2.java
package com.mkyong.date;

import java.time.*;

public class InstantExample2 {

    public static void main(String[] argv) {

		// Hard code a date time
LocalDateTime dateTime = LocalDateTime.of(2016, Month.AUGUST, 18, 6, 17, 10); System.out.println("LocalDateTime : " + dateTime); // Convert LocalDateTime to Instant, UTC+0
Instant instant = dateTime.toInstant(ZoneOffset.UTC); System.out.println("Instant : " + instant); } }
Output
Instant : 2016-08-18T06:17:10.225Z
LocalDateTime : 2016-08-18T06:17:10.225
http://www.mkyong.com/java8/java-convert-instant-to-localdatetime/
http://www.mkyong.com/tutorials/java-date-time-tutorials/

Java 8 – Convert Instant to LocalDateTime的更多相关文章

  1. Java 8 – Convert Instant to ZonedDateTime

    1. Instant -> ZonedDateTime Example to convert a Instant UTC+0 to a Japan ZonedDateTime UTC+9 Ins ...

  2. java日期互转:LocalDateTime、String、TimeStamp、Long、Instant、Date

    由于java版本的迭代,一个使用java开发的项目中可能出现多种日期对象,例如LocalDateTime.LocalDate.Date,不像C#只有一个DateTime,因此在各种日期格式或者对象之间 ...

  3. Java 8 中 Date与LocalDateTime、LocalDate、LocalTime互转

    Java 8中 java.util.Date 类新增了两个方法,分别是from(Instant instant)和toInstant()方法 // Obtains an instance of Dat ...

  4. Java 8 – Convert Map to LIST

    Java 8 – Convert Map to LIST Few Java examples to convert a Map to a List Map<String, String> ...

  5. Java 8 – Convert List to Map

    Java 8 – Convert List to Map package com.mkyong.java8 public class Hosting { private int Id; private ...

  6. Java 8 – Convert a Stream to LIST

    Java 8 – Convert a Stream to LIST package com.mkyong.java8; import java.util.List;import java.util.s ...

  7. Convert Date between LocalDateTime

    http://blog.progs.be/542/date-to-java-time Java8 has new date and time classes to “replace” the old ...

  8. Java 8 – How to format LocalDateTime

    Few examples to show you how to format java.time.LocalDateTime in Java 8. 1. LocalDateTime + DateTim ...

  9. JAVA中JDK1.8的LocalDateTime日期类的操作方法

    LocalDateTime与Date相互转换参考:https://www.cnblogs.com/pxblog/p/13745972.html 关键类 Instant:瞬时时间. LocalDate: ...

随机推荐

  1. vsphere HA内幕变化

    see aslo:http://www.yellow-bricks.com/vmware-high-availability-deepdiv/ HA Deepdive My posts on VMwa ...

  2. shell alias添加别名使用

    大家一定知道SHELL的基本用法,那么著名的命令:`ll`是代表`ls -l`,那么是怎么实现的哪?其实是添加了一个别名alias ll="ls -l" 我使用alias最多的地方 ...

  3. python2.7安装setuptools-36.6.0报ascii' codec can't decode byte 0xce in position 7问题

    解决办法1: 原文链接:http://blog.csdn.net/all_over_servlet/article/details/45112221 修改编码为gbk,修改D:\Python27\Li ...

  4. SpringBoot 分页处理

    开始主要是要使用已经设计好的数据库 -- ---------------------------------------------------- -- 用户 -- ----------------- ...

  5. Web Storage与Cookie相比存在的优势:

    (1).存储空间更大:IE8下每个独立的存储空间为10M,其他浏览器实现略有不同,但都比Cookie要大很多. (2).存储内容不会发送到服务器:当设置了Cookie后,Cookie的内容会随着请求一 ...

  6. 使用OpenNI 2获取RGBD摄像头深度信息

    NiViewer 安装好摄像头驱动和OpenNI后,在Tools文件夹中可以找到一个程序NiViewer.NiViewer的一些基本控制方法如下: 1. ESC关闭NiViewer程序 2. 右键可以 ...

  7. Linux之压缩与解压缩

    一.解压缩: tar –xvf file.tar //解压 tar包 tar -xzvf file.tar.gz //解压tar.gz tar -xjvf file.tar.bz2 //解压 tar. ...

  8. XPAGES 中CGI变量的获取

    In XPages, CGI variables are also available, but you need to write some code to get them via the JSF ...

  9. adb shell中的am pm命令

    adb shell中的am pm命令,一些自己的见解和大多数官网的翻译. am命令 am全称activity manager,你能使用am去模拟各种系统的行为,例如去启动一个activity,强制停止 ...

  10. uml中活动图与流程图的区别

    活动图定义: 活动图是UML用于对系统的动态行为建模的另一种常用工具,它描述活动的顺序,展现从一个活动到另一个活动的控制流.活动图在本质上是一种流程图. 它是UML中用于对系统动态活动建模的图形,反映 ...