Java 8 – ZonedDateTime examples
Few Java 8 java.time.ZonedDateTime
examples to show you how to convert a time zone between different countries.
Table of contents
- 1. Convert LocalDateTime to ZonedDateTime
- 2. Malaysia (UTC+08:00) -> Japan (UTC+09:00)
- 3. France, Paris (UTC+02:00, DST) -> (UTC-05:00)
- 4. References
1. Convert LocalDateTime to ZonedDateTime
The LocalDateTime
has no time zone; to convert the LocalDateTime
to ZonedDateTime
, we can use .atZone(ZoneId.systemDefault())
to create a ZonedDateTime
containing the system default time zone and convert it to another time zone using a predefined zone id or offset.
package com.mkyong.app;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
public class ZonedDateTimeExample1 {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
System.out.println(now);
System.out.println("ZoneId.systemDefault(): " + ZoneId.systemDefault());
// convert LocalDateTime to ZonedDateTime, with default system zone id
ZonedDateTime zonedDateTime = now.atZone(ZoneId.systemDefault());
// convert LocalDateTime to ZonedDateTime, with specified zoneId
ZonedDateTime europeDateTime = zonedDateTime.withZoneSameInstant(ZoneId.of("Europe/Kaliningrad"));
System.out.println(europeDateTime);
// convert LocalDateTime to ZonedDateTime, with specified off set
ZonedDateTime offSetNegative5 = now.atOffset(ZoneOffset.of("-05:00")).toZonedDateTime();
System.out.println(offSetNegative5);
// display all zone ids
//ZoneId.getAvailableZoneIds().forEach(System.out::println);
}
}
Output
2021-03-23T16:43:32.010069453
ZoneId.systemDefault(): Asia/Kuala_Lumpur
2021-03-23T10:43:32.010069453+02:00[Europe/Kaliningrad]
2021-03-23T16:43:32.010069453-05:00
2. Malaysia (UTC+08:00) -> Japan (UTC+09:00)
Review a flight detail from Malaysia Kuala Lumpur (UTC+08:00) to Japan Tokyo Haneda (UTC+09:00). The Japan Tokyo is one hour faster than Malaysia Kuala Lumpur.
Kuala Lumpur (KUL) -> Tokyo Haneda (HND)
Flight Duration : 7 hours
(KUL-Depart) 1430, 22 Aug 2016 -> 2230, 22 Aug 2016 (HND-Arrive)
The below program simulate the above flight detail if the flight is departing from Malaysia on 2016-08-22T14:30+08:00
, and it will arrive in Tokyo around 2016-08-22T22:30+09:00
, 7 hours flight duration.
package com.mkyong.app;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class ZonedDateTimeExample2 {
public static void main(String[] args) {
DateTimeFormatter format = DateTimeFormatter.ofPattern("HHmm, dd MMM uuuu");
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]
3. France, Paris (UTC+02:00, DST) -> (UTC-05:00)
This time, a flight from France, Paris (UTC+02:00, DST) to a hardcoded ZoneOffset
(UTC-05:00) time zone (e.g, New York).
France, Paris (FR) -> UTC-05:00
Flight Duration : 8 hours 10 minutes
(FR-Depart) 1430, 22 Aug 2016 -> 2230, 22 Aug 2016
package com.mkyong.app;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class ZonedDateTimeExample3 {
public static void main(String[] args) {
DateTimeFormatter format = DateTimeFormatter.ofPattern("HHmm, dd MMM uuuu");
//Convert String to LocalDateTime
String date = "2016-08-22 14:30";
LocalDateTime ldt = LocalDateTime.parse(date, DateTimeFormatter.ofPattern("uuuu-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, and the java.time.*
APIs can calculate and handle the DST correctly.
what is the output of nydate.getHours(). Its still the original unconverted hours.
This example is good for display but what about doing comparison and some calculations on the nydate and other objects createda fter zone conversion?
The
nyDateTime
is a newZonedDateTime
containing the converted-05:00
offset date time.What do you mean?