Topics Index

More Utility Classes >
Siva Nookala - 06 Oct 2016
TimeZone is an abstract class in Java, which allows us to work with time zone offsets from Greenwhich mean time (GMT), which is also referred to as Coordinated Universal Time (UTC).
TimeZone represents a time zone offset, and also figures out daylight savings(Daylight saving time (DST) or summer time is the practice of advancing clocks during the summer months that have more sunlight so that people get up later in the morning and go to bed later at night. Typically clocks are adjusted forward one hour near the start of spring and are adjusted backward in the autumn).

Creating TimeZone instance :

getDefault method of TimeZone creates a TimeZone based on the time zone where the program is running. For example, for a program running in Japan, getDefault creates a TimeZone object based on Japanese Standard Time.

Creating TimeZone with a time zone ID :

getTimeZone method of TimeZone creates a time zone with a time zone ID. For instance, the time zone ID for the U.S. Pacific Time zone is "America/Los_Angeles". So, we can get a U.S. Pacific Time TimeZone object with:
TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles");

getAvailableIDs method returns an array of supported time zone IDs. You can then choose a supported ID to get a TimeZone. If the time zone we want is not represented by one of the supported IDs, then a custom time zone ID can be specified to produce a TimeZone. The syntax of a custom time zone ID is:

         GMT Sign Hours : Minutes
         GMT Sign Hours Minutes
e.g., GMT+0530
         GMT Sign Hours
e.g., GMT-12
Hours must be between 0 to 23 and Minutes must be between 00 to 59. For example, "GMT+10" and "GMT+0010" mean ten hours and ten minutes ahead of GMT, respectively.
When creating a TimeZone, the specified custom time zone ID is normalized in the following syntax:
GMT Sign TwoDigitHours : Minutes
For example, TimeZone.getTimeZone("GMT-8").getID() returns "GMT-08:00".

Constructor :
TimeZone have only one constructor.
public TimeZone()
This constructor is the single constructor for invocation by subclass

Methods :
Method Description
static String[] getAvailableIDs() This method gets all the available IDs supported.
static TimeZone getDefault() This method gets the default TimeZone for this host.
String getDisplayName() This method returns a name of this time zone suitable for presentation to the user in the default locale.
int getDSTSavings() This method returns the amount of time to be added to local standard time to get local wall clock time.
String getID() This method gets the ID of this time zone
abstract int getOffset(int era, int year, int month, int day, int dayOfWeek, int milliseconds) This method gets the time zone offset, for current date, modified in case of daylight savings.
int getOffset(long date) This method returns the offset of this time zone from UTC at the specified date.
static TimeZone getTimeZone(String ID) This method gets the TimeZone for the given ID.
static void setDefault(TimeZone zone) This method sets the TimeZone that is returned by the getDefault method.
void setID(String ID) This method sets the time zone ID
abstract boolean useDaylightTime() This method queries if this time zone uses daylight savings time.
import java.util.*;

class TimeZoneDemo
    public static void main(String arg[])
        Calendar now = Calendar.getInstance(); // LINE A
        TimeZone timeZone = TimeZone.getTimeZone("IST"); // LINE B
        System.out.println(timeZone.getDisplayName()); // LINE C
        timeZone = TimeZone.getTimeZone("GMT");
        now.setTimeZone(timeZone); // LINE D

India Standard Time
Greenwich Mean Time


In the above program we have created a calendar instance at LINE A and a TimeZone instance with India Standard Time zone. At LINE C we dislplayed the name of the time zone using getDisplay method. At LINE D we set the time zone to Greenwich Mean Time and displayed it.

  • Replace LINE D with the below code and see the output.
    The above line throws a compilation error since setTimeZone(TimeZone) is undefined for the type TimeZone.

Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App