Java.util.TimeZone Class | Set 1
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
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.
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()
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()
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()
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()
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.
Output: Date and time of your Local Time Zone: Coordinated Universal Time, 2018-04-17 07:36:19