1. Instant -> ZonedDateTime

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

InstantZonedDateTime1.java
package com.mkyong.date;

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime; public class InstantZonedDateTime1 { public static void main(String[] argv) { // Z = UTC+0
Instant instant = Instant.now(); System.out.println("Instant : " + instant); // Japan = UTC+9
ZonedDateTime jpTime = instant.atZone(ZoneId.of("Asia/Tokyo")); System.out.println("ZonedDateTime : " + jpTime); System.out.println("OffSet : " + jpTime.getOffset()); } }
 

Output

Instant : 2016-08-18T06:17:10.225Z
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
package com.mkyong.date;

import java.time.*;

public class InstantZonedDateTime2 {

    public static void main(String[] argv) {

        LocalDateTime dateTime = LocalDateTime.of(2016, Month.AUGUST, 18, 6, 57, 38);

        // UTC+9
ZonedDateTime jpTime = dateTime.atZone(ZoneId.of("Asia/Tokyo")); System.out.println("ZonedDateTime : " + jpTime); // Convert to instant UTC+0/Z , java.time helps to reduce 9 hours
Instant instant = jpTime.toInstant(); System.out.println("Instant : " + instant); } }
 

Output

ZonedDateTime : 2016-08-18T06:57:38+09:00[Asia/Tokyo]
Instant : 2016-08-17T21:57:38Z http://www.mkyong.com/java8/java-8-convert-instant-to-zoneddatetime/
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. GCC中的内嵌汇编语言

    原文可参考:GCC中的内嵌汇编语言 一.声明   虽然Linux的核心代码大部分是用C语言编写的,但是不可避免的其中还是有一部分是用汇编语言写成的.有些汇编语言代码是直接写在汇编源程序中的,特别是Li ...

  2. Spark的运行模式(1)--Local和Standalone

    Spark一共有5种运行模式:Local,Standalone,Yarn-Cluster,Yarn-Client和Mesos. 1. Local Local模式即单机模式,如果在命令语句中不加任何配置 ...

  3. 收银台(POSBox) 配置向导

    先决条件 在开始设置您的POSBox之前, 确保你准备好了一切. 你会需要 : POSBox 2A电源适配器 一台带最新的Web浏览器的计算机或平板电脑. 可用的的SaaS或已安装零售的Odoo 设置 ...

  4. 〖Linux〗使用纯命令行来操作VBOX(宿主机不需要X11 Server)

    1. Linux安装vbox,略过 2. 查看已安装扩展插件 VBoxManage list extpacks 3. 创建一个vm: VBoxManage createvm --name " ...

  5. 〖Linux〗秒开www.stackoverflow.com,非代理方式

    stackoverflow.com就不介绍了,一种解决方法就是手动分析链接,使用nslookup得到hosts: #stack overflow 198.252.206.140 cdn.sstatic ...

  6. CSDN开源夏令营 基于Compiz的switcher插件设计与实现之编译compiz源代码

    在開始介绍之前先吐个嘈:上周我们暑期ACM集训開始了.平均下来基本上是一天一赛.有时还不止.又是多校联赛,又是CodeForces,又是TopCoder.又是BestCoder,又是AcDream.还 ...

  7. sparkmllib矩阵向量

    Spark MLlib底层的向量.矩阵运算使用了Breeze库,Breeze库提供了Vector/Matrix的实现以及相应计算的接口(Linalg).但是在MLlib里面同时也提供了Vector和L ...

  8. 使用 kubectl drain 从集群中移除节点

    对节点执行维护操作之前(例如:内核升级,硬件维护等),您可以使用 kubectl drain 安全驱逐节点上面所有的 pod.安全驱逐的方式将会允许 pod 里面的容器遵循指定的 PodDisrupt ...

  9. Linux安装配置maven以及搭建nexus私服(编写启动脚本)

    2011年07月12日16:32  下面介绍在Linux操作系统下安装配置maven和搭建nexus私服. 一.安装前的准备 下载 jdk http://www.oracle.com/technetw ...

  10. property相关参数介绍

    1.copy 使用类型 :NSString,block 2.assign使用类型 :delegate,int,float,NSInteger,bool,枚举,结构体... 3.retain使用类型 : ...