Class SimpleTimeZone in Java

digitization, transformation, binary

SimpleTimeZone Class in Java

SimpleTimeZone is a concrete subclass of TimeZone that is a simple implementation of that abstract class and is suitable for use in locales that use the Gregorian calendar. Programs do not usually need to instantiate this class directly; instead, they use one of the static “factory” methods of TimeZone to obtain a suitable TimeZone subclass.

The SimpleTimeZone class is a concrete subclass of the abstract TimeZone class. SimpleTimeZone represents a time zone offset for use with GregorianCalendar. SimpleTimeZone does not take into account the historical vicissitudes of daylight savings time, however. Instead, it assumes that the shifts to and from daylight savings time always
occur at the same time of the year.

Ads code goes here

Normally, SimpleTimeZone objects are not created directly but instead obtained by calling
TimeZone.getDefault(). This method creates a subclass of TimeZone that is appropriate for the current locale. We can also call TimeZone.getTimeZone() to obtain a TimeZone object for a specific time zone.

we should instantiate this class directly only if we need to support a time zone with nonstandard daylight savings time rules. In that case, we should use call setStartRule() and setEndRule() to specify the starting and ending dates of daylight savings time for the time zone.

The class structure of SimpleTimeZone class is given as

public class java.util.SimpleTimeZone extends java.util.TimeZone{
// Public Constructors
public SimpleTimeZone(int rawOffset, String ID);
public SimpleTimeZone(int rawOffset, String ID, int startMonth, int startDayOfWeekInMonth, int startDayOfWeek, int startTime, int endMonth, int endDayOfWeekInMonth, int endDayOfWeek, int endTime);
// Public Instance Methods
public Object clone(); // Overrides TimeZone
public boolean equals(Object obj); // Overrides Object
public int getOffset(int era, int year, int month, int day, int dayOfWeek, int millis); // Defines TimeZone
public int getRawOffset(); // Defines TimeZone
public synchronized int hashCode(); // Overrides Object
public boolean inDaylightTime(Date date); // Defines TimeZone
public void setEndRule(int month, int dayOfWeekInMonth, int dayOfWeek,int time);
public void setRawOffset(int offsetMillis); // Defines TimeZone
public void setStartRule(int month, int dayOfWeekInMonth, int dayOfWeek,int time);
public void setStartYear(int year);
public boolean useDaylightTime(); // Defines TimeZone
}

The details of the class structure are given as follows:

public SimpleTimeZone(int rawOffset, String ID);

public SimpleTimeZone(int rawOffset, String ID) constructor creates a SimpleTimeZone that uses the given offset from GMT and has the specified ID. This constructor creates a SimpleTimeZone that does not use daylight savings time.

READ  All About Class File in Java

Parameter
rawOffset – The raw offset of this time zone from GMT, in milliseconds.
ID – The ID of this time zone.

public SimpleTimeZone(int rawOffset, String ID, int startMonth,int startDayOfWeekInMonth, int startDayOfWeek,int startTime, int endMonth, int endDayOfWeekInMonth,int endDayOfWeek, int endTime);

public SimpleTimeZone(int rawOffset, String ID, int startMonth,int startDayOfWeekInMonth, int startDayOfWeek,int startTime, int endMonth, int endDayOfWeekInMonth,int endDayOfWeek, int endTime) constructor creates a SimpleTimeZone that uses the given offset from GMT, has the specified ID, and uses daylight savings time. Daylight savings time begins and ends at the specified dates and times.

For example, Brazil Eastern Time (BET) is three hours behind GMT. Daylight savings time for BET starts on the first Sunday in April at 2:00 AM and ends on the last Sunday in October, also at 2:00 A.M. To construct a TimeZone that represents BET, you would use the following:

new SimpleTimeZone(-3 * 60 * 60 * 1000, "BET",Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000, Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * 60 * 60 * 1000)

Parameter
rawOffset – The raw offset of this time zone from GMT, in milliseconds.
ID – The ID of this time zone.
startMonth – The month when daylight savings time begins.
startDayOfWeekInMonth – The week in the month when daylight savings time begins.
startDayOfWeek – The day of the week when daylight savings time begins.
startTime – The time of day when daylight savings time begins, in milliseconds.
endMonth – The month when daylight savings time ends.
endDayOfWeekInMonth – The week in the month when daylight savings time ends.
endDayOfWeek – The day of the week when daylight savings time ends.
endTime – The time of day when daylight savings time ends, in milliseconds.

public Object clone();

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

READ  Class ObjectInputStream in Java

This method returns a copy of this SimpleTimeZone.

public boolean equals(Object obj);

public boolean equals(Object obj) method returns true if obj is an instance of SimpleTimeZone, and it contains the same value as the object this method is associated with.

This method returns true if the objects are equal; false if they are not.

Parameter
obj – The object to be compared with this object.

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

public int getOffset(int era, int year, int month, int day,int dayOfWeek, int millis) method calculates an offset from GMT for the given date for this SimpleTimeZone. 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 int getRawOffset();

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

This method returns an offset from GMT, in milliseconds.

public synchronized int hashCode();

public synchronized int hashCode() method returns a hashcode for this object.

This method returns a hashcode for this SimpleTimeZone.

public boolean inDaylightTime(Date date);

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

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

Parameter
date – The date to be tested.

public void setEndRule(int month, int dayOfWeekInMonth,int dayOfWeek, int time);

public void setEndRule(int month, int dayOfWeekInMonth,int dayOfWeek, int time) method sets the time when daylight savings time ends for this SimpleTimeZone. For example, to set the end of daylight savings time to 2 A.M. on the last Sunday in October, you would use the following:

setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY,2 * 60 * 60 * 1000)

Parameter
month – The month when daylight savings time ends.
DayOfWeekInMonth – The week of the month when daylight savings time ends.
dayOfWeek – The day of the week when daylight savings time ends.
time – The time of day when daylight savings time ends, in milliseconds.

public void setRawOffset(int offsetMillis);

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

READ  A Comparison Amongst The Java IDEs: Netbeans vs Eclipse

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

public void setStartRule(int month, int dayOfWeekInMonth,int dayOfWeek, int time);

public void setStartRule(int month, int dayOfWeekInMonth,int dayOfWeek, int time) method sets the time when daylight savings time begins for this SimpleTimeZone. For example, to set the beginning of daylight savings time to 2 A.M. on the first Sunday in April, you would use the following:

setEndRule(Calendar.APRIL, 1, Calendar.SUNDAY,2 * 60 * 60 * 1000)

Parameter
month – The month when daylight savings time begins.
DayOfWeekInMonth – The week of the month when daylight savings time begins.
dayOfWeek – The day of the week when daylight savings time begins.
time – The time of day when daylight savings time begins, in milliseconds.

public void setStartYear(int year);

public void setStartYear(int year) method sets the year after which the start and end rules for daylight savings time are observed.

Parameter
year – The year when daylight savings time begins.

public boolean useDaylightTime();

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

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

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

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

SimpleTimeZone class also has inherited methods from TimeZone. They are as follows:

  • getID()
  • setID()

Share and Enjoy !

Leave a Comment

Your email address will not be published. Required fields are marked *