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
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 program for Demonstration of// getAvailableIDs() and// getAvailableIDs(int rawOffset ) methodsimportjava.util.TimeZone;publicclassTimeZoneDemo {publicstaticvoidmain(String[] args){// get all the timezones ids defined by TimeZone classString[] avilableTimezones = TimeZone.getAvailableIDs();// Print Total no of TimeZonesSystem.out.println("Total No of Time Zone Avilable");System.out.println(avilableTimezones.length);// get all the timezones whose offset is// 7200000 milliseconds means 2 hourString[] timezones = TimeZone.getAvailableIDs(7200000);// Print Total no of TimeZonesSystem.out.println("No of Time Zone having time offset 2 hour");System.out.println(timezones.length);// print all timezones namesSystem.out.println("Timezone names having time offset 2 hour");for(inti =0; i < timezones.length; i++)System.out.println(timezones[i]);}}chevron_rightfilter_noneOutput: 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 program for Demonstration of// getDefault() and getDisplayName() methodsimportjava.util.TimeZone;publicclassTimeZoneDemo {publicstaticvoidmain(String[] args){// Get your Local Time Zone Where this Program is Running.TimeZone timezone = TimeZone.getDefault();// Get the Name of Time ZoneString LocalTimeZoneDisplayName = timezone.getDisplayName();// Print the Name of Time ZoneSystem.out.println(LocalTimeZoneDisplayName);}}chevron_rightfilter_noneOutput: 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 program for Demonstration of// getTimeZone(String ID),// getDSTSavings() and getID() methodsimportjava.sql.Time;importjava.util.TimeZone;publicclassTimeZoneDemo {publicstaticvoidmain(String[] args){// creating Timezone object whose id is Europe/BerlinTimeZone timezone = TimeZone.getTimeZone("Europe/Berlin");// printing the Display Name of this timezone objectSystem.out.println("Display Name");System.out.println(timezone.getDisplayName());// getting DST in millisecondsinttimeInMiliseconds = timezone.getDSTSavings();System.out.println("\nDST of Europe/Berlin is");System.out.println(timezone.getDSTSavings());// get Id of your Default Time ZoneTimeZone defaultTimezone = TimeZone.getDefault();System.out.println("\nThe id of default Time zone is");System.out.println(timezone.getID());}}chevron_rightfilter_noneOutput: 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 program for// Demonstration of getOffset(long date),// inDaylightTime(Date date) and// observesDaylightTime() methodsimportjava.sql.Time;importjava.util.*;publicclassTimeZoneDemo {publicstaticvoidmain(String[] args){// creating Timezone object whose id is Europe/BerlinTimeZone timezone = TimeZone.getTimeZone("Europe/Berlin");// printing offset valueSystem.out.println("Offset value of Europe/Berlin:");System.out.println(timezone.getOffset(Calendar.ZONE_OFFSET));// create Date ObjectDate date =newDate(2017,04,16);// checking the date is in day light time of that Time Zone or notSystem.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 NotSystem.out.println("\nTimeZone name "+ timezone.getDisplayName());System.out.println("Observes Day Light Time");System.out.println(timezone.observesDaylightTime());}}chevron_rightfilter_noneOutput: 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 program for Demonstration of// setDefault(TimeZone zone),// setID(String ID) and clone() methodsimportjava.util.*;publicclassTimeZoneDemo {publicstaticvoidmain(String[] args){// My previous Default Time Zone isTimeZone DefaultTimeZone = TimeZone.getDefault();System.out.println("Current Default TimeZone:");System.out.println(DefaultTimeZone.getDisplayName());// Setting Europe/Berlin as your Default Time ZoneTimeZone 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/Bertimezone.setID("Eur/Ber");System.out.println("\nNew Id of Europe/Berlin is");System.out.println(timezone.getID());// create copy of a time zoneSystem.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());}}chevron_rightfilter_noneOutput: 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 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
Reference – Oracle Documentation
Recommended Posts:
- Inner Class And Anonymous Inner Class that Implements Runnable | Concurrent Programming Approach 3
- Implement Septet Class from Sextet Class in Java using JavaTuples
- Implement Decade Class from Ennead Class in Java using JavaTuples
- Implement Triplet Class with Pair Class in Java using JavaTuples
- Implement Quintet Class with Quartet Class in Java using JavaTuples
- Implement Octet Class from Septet Class in Java using JavaTuples
- Implement Pair Class with Unit Class in Java using JavaTuples
- Implement Ennead Class from Octet Class in Java using JavaTuples
- Implement Quartet Class with Triplet Class in Java using JavaTuples
- Implement Sextet Class from Quintet Class in Java using JavaTuples
- Difference between Abstract Class and Concrete Class in Java
- Using predefined class name as Class or Variable name in Java
- Java.util.TimeZone Class (Set-2) | Example On TimeZone Class
- Does JVM create object of Main class (the class with main())?
- Java.lang.Class class in Java | Set 1
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



