Java TimeZone Class



Introduction

The Java TimeZone class represents a time zone offset, and also figures out daylight savings.Following are the important points about TimeZone −

  • It takes into consideration various time zone.

  • Through the method used under this class a program running in any country, gets a TimeZone object based on that particular country's time zone.

Class declaration

Following is the declaration for java.util.TimeZone class −

public abstract class TimeZone
   extends Object
   implements Serializable, Cloneable

Field

Following are the fields for java.util.TimeZone class −

  • static int LONG − This is the style specifier for getDisplayName() indicating a long name, such as "Pacific Standard Time."

  • static int SHORT − This is the style specifier for getDisplayName() indicating a short name, such as "Pacific.Standard Time".

Class constructors

Sr.No. Constructor & Description
1

TimeZone()

This constructor is the single constructor for invocation by subclass constructors.

Class methods

Sr.No. Method & Description
1 Object clone()

This method creates a copy of this TimeZon

2 static String[] getAvailableIDs()

This method gets all the available IDs supported.

3 static TimeZone getDefault()

This method gets the default TimeZone for this host.

4 String getDisplayName()

This method returns a name of this time zone suitable for presentation to the user in the default locale.

5 int getDSTSavings()

This method returns the amount of time to be added to local standard time to get local wall clock time.

6 String getID()

This method gets the ID of this time zone

7 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.

8 abstract int getRawOffset()

This method returns the amount of time in milliseconds to add to UTC to get standard time in this time zone.

9 static TimeZone getTimeZone(String ID)

This method gets the TimeZone for the given ID.

10 boolean hasSameRules(TimeZone other)

This method returns true if this zone has the same rule and offset as another zone.

11 abstract boolean inDaylightTime(Date date)

This method queries if the given date is in daylight savings time in this time zone.

12 static void setDefault(TimeZone zone)

This method sets the TimeZone that is returned by the getDefault method.

13 void setID(String ID)

This method sets the time zone ID

14 abstract void setRawOffset(int offsetMillis)

This method sets the base time zone offset to GMT.

15 ZoneId toZoneId()

This method converts this TimeZone object to a ZoneId.

16 abstract boolean useDaylightTime()

This method queries if this time zone uses daylight savings time.

Methods inherited

This class inherits methods from the following classes −

  • java.util.Object

Getting Default Timezone for the Current Host Example

The following example shows the usage of Java TimeZone getDefault() method to get the default TimeZone object for this host. We've created a TimeZone using getDefault() method and then printed it.

package com.tutorialspoint;

import java.util.TimeZone;

public class TimeZoneDemo {
   public static void main( String args[] ) {

      // create default time zone object
      TimeZone timezonedefault = TimeZone.getDefault();      

      // checking default time zone value          
      System.out.println("Default time zone is :\n" + timezonedefault);
   }    
}

Output

Let us compile and run the above program, this will produce the following result.

Default time zone is :
sun.util.calendar.ZoneInfo[id="Asia/Calcutta",offset=19800000,dstSavings=0,useDaylight=false,transitions=7,lastRule=null]
Advertisements