How to convert java.util.TimeZone to java.time.ZoneId in Java 8 | tutorial with examples
This tutorial explains how to convert from
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
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
OUTPUT of the above code
Explanation of the code
OUTPUT of the above code
Explanation of the code
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);
}
}
ZoneId from default system TimeZone: Asia/Calcutta ZoneId from custom 'LA' TimeZone: America/Los_Angeles
- In
TimeZoneToZoneI
d class'smain()
method, we first create an instance ofjava.util.TimeZone
by fetching system's default time zone usingTimeZone.getDefault() method
.getDefault()
method returns the time zone of the system on which the JVM is running. - We chain the
getDefault()
method to theTimeZone.toZoneId()
method, which converts theTimeZone
instance to it equivalentZoneId
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 sameTimeZone.toZoneId()
method it can be converted to its equivalentZoneId
. - We first create a
TimeZone
instance forLos Angeles
, namedtimeZoneLA
. Next we convert it toZoneId
usingtoZoneId()
method. On printingzoneIdLA
’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);
}
}
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]]
- In
TimeZoneToZoneId
class'smain()
method, we first create an instance ofZoneId
for the time zone in which the system is running withZoneId.systemDefault()
method. - System's
ZoneId
is then passed as a parameter to the static methodTimeZone.getTimeZone()
which returns aTimeZone
instance, which when printed outputs a longString
value forTimeZone
begining fromsun.util.calendar.ZoneInfo[id="Asia/Calcutta"...
, which is the expected output. - Next, we do the same conversion from a
ZoneId
to aTimeZone
, but this time we start from a specificZoneId
instance forLos Angeles
. We again pass it to theTimeZone.getTimeZone()
method. The returnedTimeZone
instance is printed and the output isString
starting withsun.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.
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...Tutorials on Java 8’s new Date and Time API
Overview of Java 8's new Date and Time APIClick to Read Overview of Java 8's new Date-Time API Working with time zones in Java 8| ZonedDateTime, ZoneId tutorial with examplesClick to Read tutorial on time zone handling in Java 8 How to convert LocalDate to String and String to LocalDateClick to Read tutorial on String to LocalDate conversions How to convert java.util.Date to java.time.LocalDateClick to Read tutorial on java.util.Date to LocalDate conversion Formatting localized dates in Spanish and FrenchClick to Read date formatting in Spanish & French How to get day-of-week for a given dateHow to get day-of-week using java.time.DayOfWeek enumDate Modification using TemporalAdjuster Click to Read Tutorial on TemporalAdjusters
Overview of Java 8's new Date and Time APIClick to Read Overview of Java 8's new Date-Time API Working with time zones in Java 8| ZonedDateTime, ZoneId tutorial with examplesClick to Read tutorial on time zone handling in Java 8 How to convert LocalDate to String and String to LocalDateClick to Read tutorial on String to LocalDate conversions How to convert java.util.Date to java.time.LocalDateClick to Read tutorial on java.util.Date to LocalDate conversion Formatting localized dates in Spanish and FrenchClick to Read date formatting in Spanish & French How to get day-of-week for a given dateHow to get day-of-week using java.time.DayOfWeek enumDate Modification using TemporalAdjuster Click to Read Tutorial on TemporalAdjusters