Few Java 8 java.time.ZonedDateTime examples to show you how to convert a time zone between different countries.
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.
ZonedDateTimeExample1.java
package com.favtuts.time;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
public class ZonedDateTimeExamples {
public static void main(String[] args) {
convertLocalDateTimetoZonedDateTime();
}
static void convertLocalDateTimetoZonedDateTime() {
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
2022-06-04T23:56:45.200739
ZoneId.systemDefault(): Asia/Ho_Chi_Minh
2022-06-04T18:56:45.200739+02:00[Europe/Kaliningrad]
2022-06-04T23:56:45.200739-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.
ZonedDateTimeExample2.java
package com.favtuts.time;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class ZonedDateTimeExamples {
public static void main(String[] args) {
flightDetailFromMalaysiaToTokyo();
}
static void flightDetailFromMalaysiaToTokyo() {
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
ZonedDateTimeExample3.java
package com.favtuts.time;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class ZonedDateTimeExamples {
public static void main(String[] args) {
flightDetailFromFranceParisToNewYork();
}
static void flightDetailFromFranceParisToNewYork() {
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
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.
Download Source Code
$ git clone https://github.com/favtuts/java-core-tutorials-examples
$ cd java-basic/time