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. 【HDU 5647】DZY Loves Connecting(树DP)

    pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS ...

  2. 超具体Windows版本号编译执行React Native官方实例UIExplorer项目(多图慎入)

    ),React Native技术交流4群(458982758).请不要反复加群! 欢迎各位大牛,React Native技术爱好者加入交流!同一时候博客右側欢迎微信扫描关注订阅号,移动技术干货,精彩文 ...

  3. [转]jquery设置select选中,赋值等操作

    一.基础取值问题 例如<select class="selector"></select> 1.设置value为pxx的项选中 $(".selec ...

  4. 【DB2】SQL优化

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAA

  5. python之函数用法startswith()

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法startswith() #http://www.runoob.com/python/ ...

  6. 远程过程调用协议(RPC)

      RPC(Remote Procedure Call Protocol)——远程过程调用协议,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议.RPC协议假定某些传输协议的 ...

  7. fiddler怎么修改服务器返回参数并发送

    在进行使用fiddler中提供了较多的功能进行使用的,那么进行就可以对当前的网页中的暂停之后,在进行使用fiddler中把拦截起的后,在进行修改的服务器参数的之后,在把参数修改完成之后,把进行发送出去 ...

  8. scala object 转Class Scala强制 类型转换

    scala object 转Class Scala强制类型转换   asInstanceOf[T] 将对象类型强制转换为T类型. 还是由于泛型存在类型擦除的原因,1.asInstanceOf[Stri ...

  9. Ubuntu x86-64汇编(6)

    寻址方式 Addressing Modes 地址和值 Addresses vs Values 在64bit架构中, 地址是64bit. 访问内存内容的唯一方式就是通过方括号, 不加方括号读取的只是地址 ...

  10. 转:Delta3D Editor编译成功

    下载:     官网 www.delta3d.org 有些地方访问不了(试试这个代理:133.1.74.163 3128),我在公司就访问不了,但是住的地方就可以.但是sourceforge是可以访问 ...