转载 

Java 8 – ZonedDateTime examples

分类:    260人阅读    IT小君  2018-07-01 09:41
paris-time-zone

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

  1. Wikipedia – Daylight saving time
  2. DateTimeFormatter JavaDoc
  3. ZoneddateTime JavaDoc
  4. Time Zone in Paris
  5. Airasia – Flight information
  6. Java 8 – Convert Instant to ZonedDateTime
  7. Java – Convert date and time between timezone
  8. Java 8 – How to convert String to LocalDate
点击广告,支持我们为你提供更好的服务

小众时尚单品在线电子商务网站模板

jQuery右端悬浮带返回顶部特效

css+js实现的颜色渐变数字时钟动画特效

有机水果蔬菜HTML5网站模板

HTML5现代家居装潢公司网站模板

HTML5 Canvas竖直流动线条背景动画特效

js+css3抽奖转盘旋转点餐代码

中小型创意设计服务公司网站模板

HTML5数字产品服务公司网站模板

css鼠标跟随文字模糊特效

html5 canvas进度条圆环图表统计动画特效

响应式咖啡饮品宣传网站模板

html5图标下拉搜索框自动匹配代码

响应式时尚单品在线商城网站模板

html5 canvas彩色碎片组合球形旋转动画特效

网页设计开发公司网站模板

html5 svg夜空中星星流星动画场景特效

canvas炫酷鼠标移动文字粒子特效

响应式太阳能能源公司网站模板

现代时尚家具公司网站模板

点击广告,支持我们为你提供更好的服务
 工具推荐 更多»
点击广告,支持我们为你提供更好的服务