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. POSTGRESQL 锁表的问题

    一.找出所的语句 select wait.pid, wait.query as wait_query, wait.query_start as wait_query_start, wait.lockt ...

  2. gradle 配置及设置本地仓库

    安装Gradle 从官方网站下载安装包,解压到目录 设置环境变量 GRADLE_HOME=D:\gradle\gradle-3.4.1 PATH=;%GRADLE_HOME%\bin 设置本地仓库目录 ...

  3. JavaScript正则式练习

    使用正则式匹配第一个数字和最后一个数字,使用环视 str2 = 09051 : Fast Food Restaurants - Concession Stands/Snack Bars Delicat ...

  4. 用Eclipse进行远程Debug代码 (转)

    来自:http://blog.csdn.net/fyq891014/article/details/7534711 首先你本地Eclipse上要有和部署在远程服务器一至的项目,否则debug的时候会出 ...

  5. SSH使用Log4j

    1. 将Jar文件log4j-1.2.14.jar导入项目. 2. 在src文件夹下新建log4j.properties文件: log4j.rootLogger = debug,stdout,D,E ...

  6. Oracle单引号双重角色——字符串引用与转义

    单引号双重角色——字符串引用与转义      在ORACLE中,单引号有两个作用,一是字符串是由单引号引用,二是转义,开始学习时动态构成SQL时经常不解.单引号的使用是就近配对,即就近原则.而在单引号 ...

  7. java 获取文件后缀名(文件类型)

      java 获取文件后缀名(文件类型) CreateTime--2018年5月23日09:16:31 Author:Marydon // 获取文件名的后缀名(文件类型) String oldFile ...

  8. 【原创】kerberos无密码登录

    通常在远程客户端上登录kerbros都需要密码,在学习hadoop的时候提到了ktutil这个工具,这里将使用方法贴出来. 用到的命令如下: 1.ktutil 2.add_entry -passwor ...

  9. 安装xenapp后,非管理员连接RDP出现桌面当前不可用的解决方法

    安装完xenapp后,非管理员帐号就不能远程登录到2008服务器. 修改方法如下:1.启动 Citrix AppCenter展开citrix资源.Xenapp.<场地>.策略,右面的窗格切 ...

  10. 二维码Data Matrix简单介绍及在VS2010中的编译

    Data Matrix 二维条码原名Datacode,由美国国际资料公司(International Data Matrix, 简称ID Matrix)于1989年发明.Data-Matrix二维条码 ...