Abstract Class TimeZone in Java

0
46
digitization, transformation, binary
Spread the love

Abstract Class TimeZone in Java

The TimeZone class represents a time zone; it is used with the Calendar and DateFormat classes.

The TimeZone class is an abstract class that represents a time zone offset. In addition, the class incorporates knowledge about daylight savings time. Usually, we get a TimeZone object by calling getDefault(). This method creates a TimeZone that is appropriate for the current locale. You can also call getTimeZone() to obtain a TimeZone object for a specific time zone.

As an abstract class, TimeZone cannot be directly instantiated. Instead, you should call the static getDefault() method to obtain a TimeZone object that represents the time zone inherited from the host operating system. Or, we should call getTimeZone() (also static), passing the name of the desired zone. We can obtain a list of the supported time zone names by calling the static getAvailableIDs() method.

Once we have a TimeZone object, we can call inDaylightTime() to determine whether, for a given Date, daylight savings time is in effect for that time zone. Call getID() to obtain the name of the time zone, and call getOffset() for a given date to determine the number of milliseconds to add to GMT to convert to the time zone.

The structure of the class TimeZone is given by

public abstract class java.util.TimeZone extends java.lang.Object implements java.io.Serializable, java.lang.Cloneable {
// Default Constructor: 
public TimeZone();
// Class Methods
public static synchronized String[] getAvailableIDs(int rawOffset);
public static synchronized String[] getAvailableIDs();
public static synchronized TimeZone getDefault();
public static synchronized TimeZone getTimeZone(String ID);
public static synchronized void setDefault(TimeZone zone);
// Public Instance Methods
public Object clone(); // Overrides Object
public String getID();
public abstract int getOffset(int era, int year, int month, int day, int dayOfWeek, int milliseconds);
public abstract int getRawOffset();
public abstract boolean inDaylightTime(Date date);
public void setID(String ID);
public abstract void setRawOffset(int offsetMillis);
public abstract boolean useDaylightTime();
}

The details of the class structure are given as follows:

public TimeZone();

public TimeZone() constructor creates a TimeZone object. It is a default constructor.

public static synchronized String[] getAvailableIDs(int rawOffset);

public static synchronized String[] getAvailableIDs(int rawOffset) method returns a list of the predefined time zone IDs that have the given raw offset value from GMT. For example, both PNT and MST have an offset of GMT-07:00.

READ  How To Convert A Picture to GrayScale Image In Java

This method returns an array of strings that contains the predefined time zone IDs with the given raw offset value.

Parameter
rawOffset– int rawOffset value

public static synchronized String[] getAvailableIDs();

public static synchronized String[] getAvailableIDs() method This method returns a list of the predefined time zone IDs. Time zones are defined for the following ID values, starting
from Greenwich, England, and progressing eastward around the world:

  • GMT Greenwich Mean Time
  • ECT European Central Time
  • EET Eastern European Time
  • ART (Arabic) Egypt Standard Time
  • EAT Eastern African Time
  • MET Middle East Time
  • NET Near East Time
  • PLT Pakistan Lahore Time
  • IST India Standard Time
  • BST Bangladesh Standard Time
  • VST Vietnam Standard Time
  • CTT China Taiwan Time
  • JST Japan Standard Time
  • ACT Australia Central Time
  • AET Australia Eastern Time
  • SST Solomon Standard Time
  • NST New Zealand Standard Time
  • MIT Midway Islands Time
  • HST Hawaii Standard Time
  • AST Alaska Standard Time
  • PST Pacific Standard Time
  • PNT Phoenix Standard Time
  • MST Mountain Standard Time
  • CST Central Standard Time
  • EST Eastern Standard Time
  • IET Indiana Eastern Standard Time
  • PRT Puerto Rico and U.S. Virgin Islands Time
  • CNT Canada Newfoundland Time
  • AGT Argentina Standard Time
  • BET Brazil Eastern Time
  • CAT Central African Time

This method returns an array of strings that contains the predefined time zone IDs.

public static synchronized TimeZone getDefault();

public static synchronized TimeZone getDefault() method returns the default TimeZone object for the local system.

This method returns a TimeZone that represents the local time zone.

public static synchronized TimeZone getTimeZone(String ID);

public static synchronized TimeZone getTimeZone(String ID) method returns the TimeZone object that corresponds to the time zone with the given ID.

READ  Common Exception Classes in Java With Example

This method returns a TimeZone that represents the time zone with the given ID.

Parameter
ID – The ID of a time zone.

public static synchronized void setDefault(TimeZone zone);

public static synchronized void setDefault(TimeZone zone) method sets the TimeZone that is returned by getDefault().

Parameter
zone – The new default time zone.

public Object clone(); 

public Object clone(); method creates a copy of this TimeZone and returns it.

This method returns a copy of this TimeZone.

public String getID();

public String getID() method returns the ID string of this TimeZone.

This method returns the ID of this TimeZone.

public abstract int getOffset(int era, int year, int month, int day, int dayOfWeek, int milliseconds);

public abstract int getOffset(int era, int year, int month, int day, int dayOfWeek, int milliseconds) method calculates an offset from GMT for the given date for this TimeZone. In other words, the offset takes daylight savings time into account. The return value should be added to GMT to get local time.

This method returns an offset from GMT, in milliseconds.

Parameter
era – The era.
year – The year.
month – The month.
day – The day.
dayOfWeek – The day of the week.
millis – The time of day in milliseconds.

public abstract int getRawOffset();

public abstract int getRawOffset() method returns the raw offset from GMT for this TimeZone. In other words, the offset does not take daylight savings time into account.

This method returns an offset from GMT, in milliseconds.

public abstract boolean inDaylightTime(Date date);

public abstract boolean inDaylightTime(Date date) method returns a boolean value that indicates if the given date is in daylight savings time for this TimeZone.

READ  Class PrintStream in Java

This method returns true if the given date is between the start and end of daylight savings time for this TimeZone; false otherwise.

Parameter
date – The date to be tested.

public void setID(String ID);

public void setID(String ID) method sets the ID of this TimeZone.

Parameter
ID – The new time zone ID.

public abstract void setRawOffset(int offsetMillis);

public abstract void setRawOffset(int offsetMillis) method is used to set the raw offset value for this TimeZone.

Parameter
offsetMillis – The new raw offset from GMT, in milliseconds.

public abstract boolean useDaylightTime();

public abstract boolean useDaylightTime() method returns a boolean value that indicates whether or not this TimeZone uses daylight savings time.

This method returns true if this TimeZone uses daylight savings time; false otherwise.

Apart from these TimeZone class also has inherited methods from class- Object. They are as follows:

  • finalize()
  • hashCode()
  • notifyAll()
  • wait()
  • wait(long, int)
  • getClass()
  • notify()
  • toString()
  • wait(long)


Spread the love
I am the founder and owner of the blog - TechTravelHub.com, always love to share knowledge on test automation,tools,techniques and tips.I am a passionate coder of Java and VBScript.I also publish articles on Travel ideas and great honeymoon destinations.Apart from these, I am a gear-head,love to drive across India. I have shared lots of articles here on How to travel several parts of India.Customization of cars aka car modification is my another hobby.Get in touch with me on ani01104@gamil.com

LEAVE A REPLY

Please enter your comment!
Please enter your name here