Java Convert TimeZone to ZoneId

Tags: Java TimeZone Java ZoneId Java 8

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

How to convert TimeZone to ZoneId in Java

In Java, with a given TimeZone object we can use the TimeZone.toZoneId() method to convert it to a ZoneId object as the example Java code below.

ConvertTimeZoneToZoneId.java

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

public class ConvertTimeZoneToZoneId {
    public static void main(String... args) {
        TimeZone timeZone1 = TimeZone.getDefault();
        TimeZone timeZone2 = TimeZone.getTimeZone("Europe/Paris");

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

        System.out.println("timeZone1: " + timeZone1);
        System.out.println("timeZone2: " + timeZone2);
        System.out.println("zoneId1: " + zoneId1);
        System.out.println("zoneId2: " + zoneId2);
    }
}
The output as below.
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]]
zoneId1: Asia/Bangkok
zoneId2: Europe/Paris

Happy Coding 😊

Java Convert ZoneId to TimeZone