Java Convert ZonedDateTime to Another Time Zone
Tags: Java ZonedDateTime Java 8 Java ZoneId
In this Java core tutorial we learn how to convert a java.time.ZonedDateTime object from a specified time zone to another time zone in Java programming language.
How to convert ZonedDateTime to Another Time Zone in Java
In Java, with a given ZonedDateTime object in specified time zone we can use the ZonedDateTime.withZoneSameInstant(ZoneId zone) method to get a new ZonedDateTime object in another time zone.
In the following example Java code we show how to use the ZonedDateTime.withZoneSameInstant(ZoneId zone) method convert ZonedDateTime object in Australia/Sydney time zone to Europe/Paris time zone.
ConvertZonedDateTimeToAnotherTimeZoneExample1.java
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class ConvertZonedDateTimeToAnotherTimeZoneExample1 {
public static void main(String... args) {
// Date and time in Australia/Sydney time zone
ZonedDateTime sydneyZonedDateTime = ZonedDateTime.parse("2022-05-22T12:30:40+10:00[Australia/Sydney]");
// Convert date and time to Europe/Paris time zone
ZoneId zoneId = ZoneId.of("Europe/Paris");
ZonedDateTime parisTimeZonedDateTime = sydneyZonedDateTime.withZoneSameInstant(zoneId);
System.out.println(sydneyZonedDateTime);
System.out.println(parisTimeZonedDateTime);
}
}
2022-05-22T12:30:40+10:00[Australia/Sydney]
2022-05-22T04:30:40+02:00[Europe/Paris]
Happy Coding 😊