Java TimeZone Class Example

TimeZone is an abstract class which defines as standard time followed within a geographical area for the use with the Gregorian calendar. The TimeZone class allows working with TimeZone offset based on Greenwich Mean Time (GMT) which is referred as Coordinated Universal Time. It also sets a daylight saving time. By using getDefault TimeZone, it creates a default TimeZone for the system based on a time zone where the program is running. For example, if program running in Australia, based on Australia Standard Time, the getDefault creates a TimeZone. Like this, it gets a TimeZone based object based on particular country’s time zone.

also read:

By using getTimeZone, it’s possible to get a TimeZone along with a time zone ID. For instance, the time zone ID for Pacific Standard Time zone is abbreviated as “PST”. CST, CTT etc can be recognized for multiple time zones i.e. “Central Standard Time” or “China Standard Time”.

TimeZone Class Declaration

public abstract class TimeZone
	extends Object
		implements Serializable, Cloneable

The above code defines an object of class TimeZone which allows the serialization and colonizing of the objects. Serialization means an object can be converted to the series of bytes, which contains the information of the object’s data. Cloneable allows making the duplicate copy of the object.

TimeZone Class Fields

  • static int LONG: It is a style specifier which display the long name such as “Central European Summer Time”.
  • static int SHORT: It is a style specifier which display the short name such as “CEST”.

TimeZone Class Constructors

TimeZone( ): This is a single constructor.

TimeZone Class Example

package myproject;
	import java.util.TimeZone;
	     public class SimpleTimeZone {
	     public static void main( String args[] ){

	      TimeZone tz = TimeZone.getDefault();
	      System.out.println("Default TimeZone: " + tz);

  • The above is going to display the default TimeZone.
  • TimeZone tz = TimeZone.getDefault(); line creates a default TimeZone by using TimeZone instance tz and displays on the output.

TimeZone Class Methods

Object Clone( )It creates a duplicate copy of the specified instance of TimeZone.
static String[] getAvailableIDs( )It returns all the available id’s.
static String[] getAvailableIDs( int rawOffset)It returns all the available id’s which are specified in time zone offset.
string getDisplayName( )It displays the name of the time zone for default locale.
String getDisplayName( boolean daylight, int style)It displays the name of the time zone in a specified style for the default locale.
String getDisplayName(boolean daylight, int style, Locale locale)It defines the name of the time zone in a specified style for the specified locale.
String getDisplayName(Locale locale)It specifies the name of the time zone of specified locale.
String getID( )It returns time zone ID.
static TimeZone getTimeZone(String ID)It returns time zone for the specified id.
static void setDefault(TimeZone zone)It sets the default time zone returned by the getDefault method.
void setID(String ID)It sets the time zone ID.
int getDSTSavings( )It returns the Daylight Saving Time in milliseconds, which is the time advanced during the day time.
int getOffset(long date)It returns the offset of this TimeZone at the given time from the UTC.
abstract int getOffset(int era, int year, int month, int day, int dayOfWeek, int milliseconds)It gives the difference between local time and UTC in milliseconds for the specified date and time considering its raw offset and effect of daylight saving.
abstract int getRawOffset( )It adds time to GMT to get local time.
abstract boolean inDaylightTime(Date date)It returns true, if the given date is in daylight saving.
abstract void setRawOffset(int offsetMillis)It set the time zone offset to GMT.
abstract boolean useDaylightTime( )It returns true, if daylight time is used in the time zone.
boolean hasSameRules(TimeZone other)It compares one TimeZone with the other TimeZone and returns true, if both have the same rules and offset.

TimeZone Class Example using Methods

package myproject;
import java.util.TimeZone;
public class Timezone {
	public static void main( String args[] ){

	      TimeZone tz = TimeZone.getTimeZone("Europe/Paris");
	      TimeZone tze = TimeZone.getTimeZone("Europe/Paris");

	      String DN=tz.getDisplayName(true,1);

	      System.out.println("The displayed name :" + DN);

	      System.out.println("GMT :" +tz.getID());

	      System.out.println("value of Raw Offset :" + tz.getRawOffset());
	      System.out.println("Daylight saving time :" +tz.getDSTSavings());
	      boolean result=tz.hasSameRules(tze);

	      System.out.println("Result after comparing:" +result );
  • In the above program we have used different methods of TimeZone class such as getDisplayName( ), getTimeZone( ), getID(), getRawOffset(), getDSTSavings() and hasSameRules(tz).
  • TimeZone tz = TimeZone.getTimeZone(“Europe/Paris”); line creates a TimeZone for a specified locale.
  • String DN=tz.getDisplayName(true,1); line displays the name of the time zone in a specified style for the specified locale.
  • tz.setID(“GMT+05:00″); line sets the time zone id.
  • System.out.println(“value of Raw Offset :” + tz.getRawOffset()); statement specifies that, the time to be added to GMT to get local time.
  • System.out.println(“Daylight saving time :” +tz.getDSTSavings()); statement returns the Daylight Saving Time in milliseconds.
  • boolean result=tz.hasSameRules(tz); line makes a comparison of one TimeZone with the other TimeZone, and returns true, if both have the same rules and offset.

Java TimeZone Example



About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind