TimeZone class is used to represents a time zone offset, and also figures out daylight savings.
What is a Time Zone and Time Offset?
“Time Zone” is used to describe the current time for different areas of the world. It refers to one of the specific regions out of the 24 total regions in the world that are divided up by longitude. Within each one of those regions, a standard version of time is maintained.
- The different time zones are calculated based on their relation to the coordinated universal time or UTC.
- A time offset is an amount of time subtracted from or added to Universal Time) time to get the current civil time, whether it is standard time or daylight-saving time (DST).
- We divide the whole earth east to west into 24 different regions based on longitude so each region is 15 degrees wider. So, there are 24 different Time zones available on earth. Each time zone is 15 degrees wide and there’s a one-hour difference between each one.
- Depending on the distance east or west from the Greenwich Meridian you must either add or subtract the appropriate time for every 15-degree interval in Longitude.
For Example : To find the time zone in hours of a particular location, you can take the longitude in degrees and divide it by 15. So, for example, 105° E would be 105/15 which equals 7. That translates to the time zone being 7 hours ahead of UTC or GMT time, which can also be labelled as UTC+7. Where 7 is a time offset for that location.
TimeZone Class in Java
Class declaration
public abstract class TimeZone extends Object implements Serializable, Cloneable
Methods of TimeZone Class :
- getAvailableIDs() – Using this method you can get all the available Time Zone IDs.
Syntax : public static String[] getAvailableIDs()
- getAvailableIDs (int rawOffset) – Using this method you can get an array of IDs, where the time zone for that ID has the specified GMT offset in milliseconds.
Syntax : public static String[] getAvailableIDs(int rawOffset) Parameters: rawOffset - the given time zone GMT offset in milliseconds.
Java
// Java program for Demonstration of // getAvailableIDs() and // getAvailableIDs(int rawOffset ) methods import java.util.TimeZone; public class TimeZoneDemo { public static void main(String[] args) { // get all the timezones ids defined by TimeZone class String[] availableTimezones = TimeZone.getAvailableIDs(); // Print Total no of TimeZones System.out.println( "Total No of Time Zone Available" ); System.out.println(availableTimezones.length); // get all the timezones whose offset is // 7200000 milliseconds means 2 hour String[] timezones = TimeZone.getAvailableIDs( 7200000 ); // Print Total no of TimeZones System.out.println( "No of Time Zone having time offset 2 hour" ); System.out.println(timezones.length); // print all timezones names System.out.println( "Timezone names having time offset 2 hour" ); for ( int i = 0 ; i < timezones.length; i++) System.out.println(timezones[i]); } } |
Output: Total No of Time Zone Available 628 No of Time Zone having a time offset 2 hour 43 Timezone names having a time offset 2 hour ART Africa/Blantyre Africa/Bujumbura Africa/Cairo...... ............
- getDefault() – Using this method you can get the TimeZone of place where Program is Running.
Syntax : public static TimeZone getDefault()
- getDisplayName() – Method returns a long standard time name of initialize TimeZone.
Syntax : public final String getDisplayName()
Java
// Java program for Demonstration of // getDefault() and getDisplayName() methods import java.util.TimeZone; public class TimeZoneDemo { public static void main(String[] args) { // Get your Local Time Zone Where this Program is Running. TimeZone timezone = TimeZone.getDefault(); // Get the Name of Time Zone String LocalTimeZoneDisplayName = timezone.getDisplayName(); // Print the Name of Time Zone System.out.println(LocalTimeZoneDisplayName); } } |
Output: Coordinated Universal Time
- getTimeZone(String ID) – This method is used to get the TimeZone for the given ID.
Syntax :public static TimeZone getTimeZone(String ID) Parameters: ID - the ID for a TimeZone.
- getDSTSavings() – Method returns the amount of time to be added to local standard time to get local wall clock time.
Syntax : public int getDSTSavings()
- getID() – This method is Used to Get the ID of this time zone.
Syntax : public String getID()
Java
// Java program for Demonstration of // getTimeZone(String ID), // getDSTSavings() and getID() methods import java.sql.Time; import java.util.TimeZone; public class TimeZoneDemo { public static void main(String[] args) { // creating Timezone object whose id is Europe/Berlin TimeZone timezone = TimeZone.getTimeZone( "Europe/Berlin" ); // printing the Display Name of this timezone object System.out.println( "Display Name" ); System.out.println(timezone.getDisplayName()); // getting DST in milliseconds int timeInMilliseconds = timezone.getDSTSavings(); System.out.println( "\nDST of Europe/Berlin is" ); System.out.println(timezone.getDSTSavings()); // get Id of your Default Time Zone TimeZone defaultTimezone = TimeZone.getDefault(); System.out.println( "\nThe id of default Time zone is" ); System.out.println(timezone.getID()); } } |
Output: Display Name Central European Time DST of Europe/Berlin is 3600000 The id of default Time zone is Europe/Berlin
- getOffset(long date) – The method is used to return the offset of this time zone from UTC at the passed date in method.
Syntax : the method is used to return the offset of this time zone from UTC at the passed date in method. Parameters: date - the date represented in milliseconds since January 1, 1970 00:00:00 GMT
- inDaylightTime(Date date) – This method returns true if the given date is in Daylight Saving Time in this time zone else false.
Syntax :Syntax : public abstract boolean inDaylightTime(Date date) Parameters:date - the given Date.
- observesDaylightTime() – This method returns true if this TimeZone is currently in Daylight Saving Time, or if a transition from Standard Time to Daylight Saving Time occurs at any future time.
Syntax :public boolean observesDaylightTime()
Java
// Java program for // Demonstration of getOffset(long date), // inDaylightTime(Date date) and // observesDaylightTime() methods import java.sql.Time; import java.util.*; public class TimeZoneDemo { public static void main(String[] args) { // creating Timezone object whose id is Europe/Berlin TimeZone timezone = TimeZone.getTimeZone( "Europe/Berlin" ); // printing offset value System.out.println( "Offset value of Europe/Berlin:" ); System.out.println(timezone.getOffset(Calendar.ZONE_OFFSET)); // create Date Object Date date = new Date( 2017 , 04 , 16 ); // checking the date is in day light time of that Time Zone or not System.out.println( "\nDate 16/04/2017 is in Day Light Time of" ); System.out.println( "Timezone: timezone.getDisplayName()" ); System.out.println(timezone.inDaylightTime(date)); // check this Time Zone observes Day Light Time or Not System.out.println( "\nTimeZone name " + timezone.getDisplayName()); System.out.println( "Observes Day Light Time" ); System.out.println(timezone.observesDaylightTime()); } } |
Output: Offset value of Europe/Berlin: 3600000 Date 16/04/2017 is in Day Light Time of Timezone: timezone.getDisplayName() true TimeZone name Central European Time Observes Day Light Time true
- setDefault(TimeZone zone) – It is used to set the TimeZone that is returned by the getDefault method.
Syntax : public static void setDefault(TimeZone zone) Parameters: zone - the new default time zone
- setID(String ID) – It is used to set the time zone ID.
Syntax :public void setID(String ID) Parameters: ID - the new time zone ID.
- clone() – This method used to create copy of this TimeZone
Syntax : public Object clone()
Java
// Java program for Demonstration of // setDefault(TimeZone zone), // setID(String ID) and clone() methods import java.util.*; public class TimeZoneDemo { public static void main(String[] args) { // My previous Default Time Zone is TimeZone DefaultTimeZone = TimeZone.getDefault(); System.out.println( "Current Default TimeZone:" ); System.out.println(DefaultTimeZone.getDisplayName()); // Setting Europe/Berlin as your Default Time Zone TimeZone timezone = TimeZone.getTimeZone( "Europe/Berlin" ); timezone.setDefault(timezone); TimeZone NewDefaultTimeZone = TimeZone.getDefault(); System.out.println( "\nNew Default TimeZone:" ); System.out.println(NewDefaultTimeZone.getDisplayName()); // change Id Europe/Berlin to Eur/Ber timezone.setID( "Eur/Ber" ); System.out.println( "\nNew Id of Europe/Berlin is" ); System.out.println(timezone.getID()); // create copy of a time zone System.out.println( "\nOriginal TimeZone ID:" ); System.out.println(timezone.getID()); TimeZone clonedTimezone = (TimeZone)timezone.clone(); System.out.println( "Cloned TimeZone ID:" ); System.out.println(clonedTimezone.getID()); } } |
Output: Current Default TimeZone: India Standard Time New Default TimeZone: Central European Time New Id of Europe/Berlin is Eur/Ber Original TimeZone ID: Eur/Ber Cloned TimeZone ID: Eur/Ber
Example : Print the Date and Time for Any Given Input Time Zone Where Program is Running.
Java
// Java program to illustrate // java.util.timezone class import java.text.*; import java.util.*; public class TimeZoneDemo { public static void main(String[] args) { // Get your Local Time Zone Where this Program is Running. TimeZone timezone = TimeZone.getDefault(); // Get the Name of Time Zone String LocalTimeZoneName = timezone.getDisplayName(); // Initialize your Date Object and Date Format to represent your Date Date date = new Date(); DateFormat dformat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ); // set your local time Zone to your Date Format time Zone dformat.setTimeZone(timezone); // Print Date and Time for your Time Zone System.out.println( "Date and time of your Local Time Zone:" ); System.out.println(LocalTimeZoneName + ", " + dformat.format(date)); } } |
Output: Date and time of your Local Time Zone: Coordinated Universal Time, 2018-04-17 07:36:19