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. 【树莓派】使用VNC进行远程控制

    之前有进行过VNC以及xrdp连接树莓派,并成功了. 这里看到一篇比较新的,基于mac的连接,文章转载收藏,实践可参考. 这一课里我们将学习如何在树莓派上安装和使用VNC.它可以使你通过图形界面的方式 ...

  2. 关于微服务、SOA、以及API的理解

    现在微服务.SOA.RESTful API设计等在各大公司很流行.微服务(micro services)这个概念不是新概念,很多公司已经在实践了,例如亚马逊.Google.FaceBook,Aliba ...

  3. C++ 第七课 标准 C I/O

    clearerr() 清除错误 fclose() 关闭一个文件 feof() 如果到达文件尾(end-of-file)返回"True"(真) ferror() 检查一个文件错误 f ...

  4. 在CentOS7(虚拟机)下通过源码安装Postgresql10以及基本配置

    操作系统:CentOS7 安装文件:postgresql-10.0.tar.gz 系统环境:gcc.Python 1:源码安装 [postgres@localhost ~]# tar zxvf pos ...

  5. listview下拉刷新 上拉(滑动分页)加载更多

    最 近做的类似于微博的项目中,有个Android功能要使用到listview的向下拉刷新来刷新最新消息,向上拉刷新(滑动分页)来加载更多.新浪微博就是使用这种方式的典型.当用户从网络上读取微博的时候, ...

  6. 单目视觉里程计 mono vo

    之前为了修改svo进行了一些不同的尝试,两个视频demo在以下. 效果1 视频链接: https://v.qq.com/x/page/d0383rpx3ap.html 在不同数据集上測试 效果2 视频 ...

  7. VREP中的力触觉设备接口(CHAI3D)

    力反馈技术是一种新型的人机交互技术,它允许用户借助力反馈设备触碰.操纵计算机生成的虚拟环境中的物体,并感知物体的运动和相应的力反馈信息,实现人机力觉交互.虽然传统的鼠标.键盘.触摸屏等交互手段可以满足 ...

  8. XPAGES 中CGI变量的获取

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

  9. php Socket表单提交学习一下

    <?php //发送请求指定的页面 $file = "1.php"; $filename = "gitignore.txt"; //文件名 $path = ...

  10. 修改windows默认的远程连接端口

    打开注册表,找到以下路径项,并修改为你想要的端口,重启服务器即可. HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Terminal Serve ...