1. Instant -> ZonedDateTime

Example to convert a Instant UTC+0 to a Japan ZonedDateTime UTC+9

InstantZonedDateTime1.java
  1. package com.mkyong.date;
  2. import java.time.Instant;
  3. import java.time.ZoneId;
  4. import java.time.ZonedDateTime;
  5. public class InstantZonedDateTime1 {
  6. public static void main(String[] argv) {
  7. // Z = UTC+0
  8. Instant instant = Instant.now();
  9. System.out.println("Instant : " + instant);
  10. // Japan = UTC+9
  11. ZonedDateTime jpTime = instant.atZone(ZoneId.of("Asia/Tokyo"));
  12. System.out.println("ZonedDateTime : " + jpTime);
  13. System.out.println("OffSet : " + jpTime.getOffset());
  14. }
  15. }
 

Output

  1. Instant : 2016-08-18T06:17:10.225Z
  2. LocalDateTime : 2016-08-18T06:17:10.225

2. ZonedDateTime -> Instant

Convert the Japan ZonedDateTime UTC+9 back to a Instant UTC+0/Z, review the result, the offset is taken care automatically.

InstantZonedDateTime2.java
  1. package com.mkyong.date;
  2. import java.time.*;
  3. public class InstantZonedDateTime2 {
  4. public static void main(String[] argv) {
  5. LocalDateTime dateTime = LocalDateTime.of(2016, Month.AUGUST, 18, 6, 57, 38);
  6. // UTC+9
  7. ZonedDateTime jpTime = dateTime.atZone(ZoneId.of("Asia/Tokyo"));
  8. System.out.println("ZonedDateTime : " + jpTime);
  9. // Convert to instant UTC+0/Z , java.time helps to reduce 9 hours
  10. Instant instant = jpTime.toInstant();
  11. System.out.println("Instant : " + instant);
  12. }
  13. }
 

Output

  1. ZonedDateTime : 2016-08-18T06:57:38+09:00[Asia/Tokyo]
  2. Instant : 2016-08-17T21:57:38Z
  3. http://www.mkyong.com/java8/java-8-convert-instant-to-zoneddatetime/
  1. http://www.mkyong.com/tutorials/java-date-time-tutorials/

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

  1. Java 8 – Convert Instant to LocalDateTime

    Java 8 examples to show you how to convert from Instant to LocalDateTime 1. Instant -> LocalDateT ...

  2. 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> ...

  3. Java 8 – Convert List to Map

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

  4. 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 ...

  5. java 8时间使用LocalDateTime,ZonedDateTime,LocalDate

    前言 java 8的时间已经能够满足日常的使用,也方便理解.joda-time作为一个有优秀的时间组件也不得不告知使用者在java 8以后使用自带的时间 LocalDateTime以及ZonedDat ...

  6. MyEclipse中将普通Java项目convert(转化)为Maven项目

    在MyEclipse10中将Maven项目转成普通Java项目后,想将Java项目转成Maven项目,结果一下子傻眼了.根本就没有攻略中提到的config标签.仔细一看,喵咪的,人家用的是Eclips ...

  7. Java——Read/convert an InputStream to a String

    获取 InputStream 并将其转换为String的简单方法. 添加commons-io-2.4.jar import java.io.IOException; import java.io.In ...

  8. Java-convert between INT and STRING

    int -> String 三种写法 String s = 43 + ""; String s = String.valueOf(43); String s = Intege ...

  9. Java 8 Date Time API Example Tutorial – LocalDate, Instant, LocalDateTime, Parse and Format

    参考 Java 8 Date and Time API is one of the most sought after change for developers. Java has been mis ...

随机推荐

  1. 使用com.jayway.jsonpath.JsonPath包进行JSON的快速解析、设置值需要注意的性能提升方法

    一.包地址 1.Maven:http://mvnrepository.com/artifact/com.jayway.jsonpath/json-path <!-- https://mvnrep ...

  2. 修改浏览器Cookie

    Firefox:安装 Web Developer 插件,装完重启之后会出现一排新的工具栏,点击 Cookies -> Add Cookie… 即可增加/修改一个Cookie了. Opera:Op ...

  3. Hibernate学习笔记五:反向工程快速开发

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6768513.html  一:反向工程 Myeclipse提供由 数据库表 生成 java pojo 和 hib ...

  4. 索引快速扫描(index fast full scan)

    一.索引快速扫描(index fast full scan) 索引快速全扫描(INDEX FAST FULL SCAN)和索引全扫描(INDEX  FULL SCAN)极为类似,它也适用于所有类型的B ...

  5. 每天五个java相关面试题(7)--线程篇

    首先关于线程能够先看我之前这篇博文熟悉一下线程生命周期的图示以及文字说明 好開始了: 1.java 中有几种方法能够实现一个线程? 答:有两种实现方法.各自是继承 Thread类与实现Runnable ...

  6. V-rep学习笔记:机器人模型创建1—模型简化

    要进行机器人仿真首先需要得到机器人的几何模型.我们可以直接通过VREP中提供的基本几何体来搭建一个简易的机器人[Menu bar --> Add --> Primitive shape - ...

  7. 给mysql配置phpmyadmin可视化管理工具

    1.配置IIS管理器1.控制面板——程序——程序和功能——启用或关闭windows功能并稍稍等候2.internet information services3.勾选ftp服务器.web管理工具.应用 ...

  8. DUBBO本地搭建及小案例 (转)

    DUBBO的介绍部分我这里就不介绍了,大家可参考官方文档. DUBBO的注册中心安装 DUBBO的注册中心支持好几种,公司用到zookeeper注册中心,所以我这边只说明zookeeper注册中心如何 ...

  9. redis配置笔记

    #cd /opt#tar -zxvf redis-4.0.6.tar.gz#cd redis-4.0.6#make #cd src#make install PREFIX=/usr/local/red ...

  10. java服务端微信小程序支付

    发布时间:2018-10-05   技术:springboot+maven   概述 java微信小程序demo支付只需配置支付一下参数即可运行 详细 代码下载:http://www.demodash ...