Java 8 – ZonedDateTime examples
编辑教程
上一节 : Java 8 – Convert Date to LocalDate and LocalDateTime
下一节 : Java 8 – How to format LocalDateTime
Few java.time.ZonedDateTime
examples to show you how to convert a time zone between different countries.
1. Malaysia (KUL) -> Japan (HND)
Review a flight information from Malaysia Kuala Lumpur (UTC+08:00) to Japan Tokyo Haneda (UTC+09:00)
---Flight Detail---
Kuala Lumpur (KUL) -> Tokyo Haneda (HND)
Flight Duration : 7 hours
(KUL-Depart) 1430, 22 Aug 2016 -> 2230, 22 Aug 2016 (HND-Arrive)
P.S Japan Tokyo is one hour faster than Malaysia Kuala lumpur
DifferentTimeZoneExample1.java
package com.mkyong.timezone;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class DifferentTimeZoneExample1 {
public static void main(String[] args) {
DateTimeFormatter format = DateTimeFormatter.ofPattern("HHmm, dd MMM yyyy");
LocalDateTime ldt = LocalDateTime.of(2016, Month.AUGUST, 22, 14, 30);
System.out.println("LocalDateTime : " + format.format(ldt));
//UTC+8
ZonedDateTime klDateTime = ldt.atZone(ZoneId.of("Asia/Kuala_Lumpur"));
System.out.println("Depart : " + format.format(klDateTime));
//UTC+9 and flight duration = 7 hours
ZonedDateTime japanDateTime = klDateTime.withZoneSameInstant(ZoneId.of("Asia/Tokyo")).plusHours(7);
System.out.println("Arrive : " + format.format(japanDateTime));
System.out.println("\n---Detail---");
System.out.println("Depart : " + klDateTime);
System.out.println("Arrive : " + japanDateTime);
}
}
Output
LocalDateTime : 1430, 22 Aug 2016
Depart : 1430, 22 Aug 2016
Arrive : 2230, 22 Aug 2016
---Detail---
Depart : 2016-08-22T14:30+08:00[Asia/Kuala_Lumpur]
Arrive : 2016-08-22T22:30+09:00[Asia/Tokyo]
2. France, Paris -> -05:00
Another time zone example from France, Paris (UTC+02:00, DST) to a hard coded (UTC-05:00) time zone (e.g New York)
---Flight Detail---
France, Paris -> UTC-05:00
Flight Duration : 8 hours 10 minutes
(Depart) 1430, 22 Aug 2016 -> 1540, 22 Aug 2016 (Arrive)
DifferentTimeZoneExample2.java
package com.mkyong.timezone;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class DifferentTimeZoneExample2 {
public static void main(String[] args) {
DateTimeFormatter format = DateTimeFormatter.ofPattern("HHmm, dd MMM yyyy");
//Convert String to LocalDateTime
String date = "2016-08-22 14:30";
LocalDateTime ldt = LocalDateTime.parse(date, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"));
System.out.println("LocalDateTime : " + format.format(ldt));
//Paris, 2016 Apr-Oct = DST, UTC+2, other months UTC+1
//UTC+2
ZonedDateTime parisDateTime = ldt.atZone(ZoneId.of("Europe/Paris"));
System.out.println("Depart : " + format.format(parisDateTime));
//hard code a zoneoffset like this, UTC-5
ZoneOffset nyOffSet = ZoneOffset.of("-05:00");
ZonedDateTime nyDateTime = parisDateTime.withZoneSameInstant(nyOffSet).plusHours(8).plusMinutes(10);
System.out.println("Arrive : " + format.format(nyDateTime));
System.out.println("\n---Detail---");
System.out.println("Depart : " + parisDateTime);
System.out.println("Arrive : " + nyDateTime);
}
}
Output
LocalDateTime : 1430, 22 Aug 2016
Depart : 1430, 22 Aug 2016
Arrive : 1540, 22 Aug 2016
---Detail---
Depart : 2016-08-22T14:30+02:00[Europe/Paris]
Arrive : 2016-08-22T15:40-05:00
Daylight Saving Time (DST)
Paris, normally UTC+1 has DST (add one hour = UTC+2) from 27/mar to 30/Oct, 2016. Review the above output, the java.time
is able to calculate and handle the DST correctly.
References
上一节 : Java 8 – Convert Date to LocalDate and LocalDateTime
下一节 : Java 8 – How to format LocalDateTime
Mos固件,小电视必刷固件
ES6 教程
Vue.js 教程
JSON 教程
jQuery 教程
HTML 教程
HTML 5 教程
CSS 教程
CSS3 教程
JavaScript 教程
DHTML 教程
JSON在线格式化工具
JS在线运行
JSON解析格式化
jsfiddle中国国内版本
JS代码在线运行
PHP代码在线运行
Java代码在线运行
C语言代码在线运行
C++代码在线运行
Python代码在线运行
Go语言代码在线运行
C#代码在线运行
JSRUN闪电教程系统是国内最先开创的教程维护系统, 所有工程师都可以参与共同维护的闪电教程,让知识的积累变得统一完整、自成体系。
大家可以一起参与进共编,让零散的知识点帮助更多的人。
X
选择支付方式:
立即支付
¥
9.99
无法付款,请点击这里
金额: 0 元
备注:
转账时请填写正确的金额和备注信息,到账由人工处理,可能需要较长时间
备注:
转账时请填写正确的金额和备注信息,到账由人工处理,可能需要较长时间
如有疑问请联系QQ:565830900
正在生成二维码, 此过程可能需要15秒钟