JB Header
How to convert java.util.TimeZone to java.time.ZoneId in Java 8 | tutorial with examples
This tutorial explains how to convert from java.util.TimeZone to java.time.ZoneId.

With Java 8's new Date and Time APIClick to Read an overview of Java 8's new Date-Time API the time zone handling classes have also been revamped. The primary identifier of a time zone in Java 8 is the java.time.ZoneId class, while until Java 7 this responsibility was borne by java.util.TimeZone class.

In this tutorial I will be explaining how to convert between the old and new time zone classes. First we will be seeing how to convert TimeZone to ZoneId, which will be followed by looking at the reverse conversion, i.e. from a ZoneId to TimeZone.
Example showing conversion from java.util.TimeZone to java.time.ZoneId
package com.javabrahman.java8.time;
import java.time.ZoneId;
import java.util.TimeZone;
public class TimeZoneToZoneId {
  public static void main(String args[]) {

    //Converting system's 'default' java.util.TimeZone to java.time.ZoneId
    ZoneId zoneId = TimeZone.getDefault().toZoneId();
    System.out.println("ZoneId from default system TimeZone: " + zoneId);

    //Converting custom defined java.util.TimeZone to java.time.ZoneId
    TimeZone timeZoneLA = TimeZone.getTimeZone("America/Los_Angeles");
    ZoneId zoneIdLA = timeZoneLA.toZoneId();
    System.out.println("ZoneId from custom 'LA' TimeZone: " + zoneIdLA);

  }
}
 OUTPUT of the above code
ZoneId from default system TimeZone: Asia/Calcutta
ZoneId from custom 'LA' TimeZone: America/Los_Angeles
Explanation of the code
  • In TimeZoneToZoneId class's main() method, we first create an instance of java.util.TimeZone by fetching system's default time zone using TimeZone.getDefault() method. getDefault() method returns the time zone of the system on which the JVM is running.
  • We chain the getDefault() method to the TimeZone.toZoneId() method, which converts the TimeZone instance to it equivalent ZoneId instance.
  • We print the zoneId, which outputs "Asia/Calcutta", i.e the time zone for India where this code was run.
  • Next example above is for the case where a TimeZone instance is already created, then using the same TimeZone.toZoneId() method it can be converted to its equivalent ZoneId.
  • We first create a TimeZone instance for Los Angeles, named timeZoneLA. Next we convert it to ZoneId using toZoneId() method. On printing zoneIdLA’s value, the output is "America/Los_Angeles" which is the expected output.
Example showing conversion from java.time.ZoneId to java.util.TimeZone
package com.javabrahman.java8.time;
import java.time.ZoneId;
import java.util.TimeZone;
public class ZoneIdToTimeZone {
  public static void main(String args[]) {

    //Convert system's 'default' java.time.ZoneId to java.util.TimeZone
    TimeZone timeZone = TimeZone.getTimeZone(ZoneId.systemDefault());
    System.out.println("TimeZone from system's default ZoneId: " + timeZone);

    //Convert custom java.time.ZoneId to java.util.TimeZone
    TimeZone timeZoneLA = TimeZone.getTimeZone(ZoneId.of("America/Los_Angeles"));
    System.out.println("TimeZone from custom 'LA' ZoneId: " + timeZoneLA);
  }
}
 OUTPUT of the above code
TimeZone from system's default ZoneId: sun.util.calendar.ZoneInfo[id="Asia/Calcutta",offset=19800000,dstSavings=0,useDaylight=false,transitions=6,lastRule=null]

TimeZone from custom 'LA' ZoneId: sun.util.calendar.ZoneInfo[id="America/Los_Angeles",offset=-28800000,dstSavings=3600000,useDaylight=true,transitions=185,lastRule=java.util.SimpleTimeZone[id=America/Los_Angeles,offset=-28800000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=8,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=3,endMonth=10,endDay=1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]]
Explanation of the code
  • In TimeZoneToZoneId class's main() method, we first create an instance of ZoneId for the time zone in which the system is running with ZoneId.systemDefault() method.
  • System's ZoneId is then passed as a parameter to the static method TimeZone.getTimeZone() which returns a TimeZone instance, which when printed outputs a long String value for TimeZone begining from sun.util.calendar.ZoneInfo[id="Asia/Calcutta"..., which is the expected output.
  • Next, we do the same conversion from a ZoneId to a TimeZone, but this time we start from a specific ZoneId instance for Los Angeles. We again pass it to the TimeZone.getTimeZone() method. The returned TimeZone instance is printed and the output is String starting with sun.util.calendar.ZoneInfo[id="America/Los_Angeles"..., which is the expected output.
  • In case you wish to understand time zone handling in Java 8 completely, then you can refer the Java 8 working with time zonesClick to read tutorial on time zone handling in Java 8 tutorial.
In the above tutorial we looked at how to convert from java.util.TimeZone to java.time.ZoneId in Java 8, along with the reverse conversion. Listed below are articles on to Date and Time API published on JavaBrahman for further reading...