Java Convert ZoneId to TimeZone

Tags: Java ZoneId Java TimeZone Java 8

In this Java core tutorial we learn how to convert a java.time.ZoneId object to a java.util.TimeZone object in Java programming language.

How to convert ZoneId to TimeZone in Java

In Java, with a given ZoneId object we can use the TimeZone.getTimeZone(ZoneId zoneId) static method to instantiate a new TimeZone object from ZoneId value as the example Java code below.

ConvertZoneIdToTimeZoneExample1.java

import java.time.ZoneId;
import java.util.TimeZone;

public class ConvertZoneIdToTimeZoneExample1 {
    public static void main(String... args) {
        ZoneId zoneId1 = ZoneId.systemDefault();
        ZoneId zoneId2 = ZoneId.of("Europe/Paris");

        // Convert ZoneId object to TimeZone object  
        TimeZone timeZone1 = TimeZone.getTimeZone(zoneId1);
        TimeZone timeZone2 = TimeZone.getTimeZone(zoneId2);

        System.out.println("zoneId1: " + zoneId1);
        System.out.println("zoneId2: " + zoneId2);
        System.out.println("timeZone1: " + timeZone1);
        System.out.println("timeZone2: " + timeZone2);
    }
}
The output as below.
zoneId1: Asia/Bangkok
zoneId2: Europe/Paris
timeZone1: sun.util.calendar.ZoneInfo[id="Asia/Bangkok",offset=25200000,dstSavings=0,useDaylight=false,transitions=3,lastRule=null]
timeZone2: sun.util.calendar.ZoneInfo[id="Europe/Paris",offset=3600000,dstSavings=3600000,useDaylight=true,transitions=184,lastRule=java.util.SimpleTimeZone[id=Europe/Paris,offset=3600000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=2,startMonth=2,startDay=-1,startDayOfWeek=1,startTime=3600000,startTimeMode=2,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=3600000,endTimeMode=2]]

Happy Coding 😊

Java Convert TimeZone to ZoneId