Java SimpleTimeZone Class Example

SimpleTimeZone is a subclass of TimeZone which is defined as standard time followed within a geographical area for the use with the Gregorian calendar. SimpleTimeZone class extends the Super Class TimeZone. This class takes an offset from GMT, also known as raw offset to set the start and end rules for the Daylight Saving Time. Daylight Saving Time is the time advanced during day to save the day light.

also read:

To define a SimpleTimeZone with the Daylight Saving Time, certain methods are implemented such as start-method and end-method for the day when the daylight saving starts and ends. These are given with the combination of month, day-of-month and day-of-week. This class does not handle historical changes and useful for determining whether daylight savings time is in effect.

SimpleTimeZone Class Declaration

public class SimpleTimeZone extends TimeZone

SimpleTimeZone Class Fields

FieldDescription
Standard_TimeIt is of static int type, and is constant for the start and end mode for a defined Standard time.
Utc_TimeIt is of static int type, and is constant for the start and end mode for a defined UTC time.
Wall_TimeIt is of static int type, and is constant for the start and end mode for a time defined as wall clock time.

SimpleTimeZone Class Constructors

ConstructorDescription
SimpleTimeZone (int rawOffset, String ID)It constructs an object of SimpleTimeZone with the base TimeZone which corresponds to offset from GMT and TimeZone id. It does not consider the day light saving method.
SimpleTimeZone (int rawOffset, String ID, int startMonth, int startDay, int startDayofWeek, int startTime, int endMonth, int endDayofWeek, int endTime)It constructs an object of SimpleTimeZone with the base TimeZone which corresponds to offset from GMT and TimeZone id and also consider the methods of starting and ending of the daylight time.
SimpleTimeZone (int rawOffset, String ID, int startMonth, int startDay, int startDayofWeek, int startTime, int endMonth, int endDayofWeek, int endTime, int dstSavings)It constructs an object of SimpleTimeZone with the base TimeZone which corresponds to offset from GMT and TimeZone id and considers the methods of starting and ending of the daylight time along with the number of milliseconds saved during the daylight saving time.
SimpleTimeZone (int rawOffset, String ID, int startMonth, int startDay, int startDayofWeek, int startTime, int endMonth, int endDayofWeek, int endTime, int endTimeMode, int dstSavings)It constructs an object of SimpleTimeZone with the base TimeZone which corresponds to offset from GMT and TimeZone id , considers the methods of starting and ending of the daylight time, number of milliseconds saved during the daylight saving time and have additional functionality which includes the end time mode.

Simple Example of SimpleTimeZone Class

import java.util.*;
public class Simple_SimpleTimeZone {

	public static void main(String[] args) {

SimpleTimeZone s1 = new SimpleTimeZone (730,"Australia");

	System.out.println ("SimpleTimeZone representation of s1: " + s1);

	}

}
 
  • The above program briefs the simple example for SimpleTimeZone, which gives the information’s like id=Australia,offset=730,dstSavings=3600000,useDaylight=false,startYear=0,startMode=0,startMonth=0,startDay=0,startDayOfWeek=0,startTime=0,startTimeMode=0,endMode=0,endMonth=0,endDay=0,endDayOfWeek=0,endTime=0,endTimeMode=0.
  • SimpleTimeZone s1 = new SimpleTimeZone (730,”Australia”); line creates the SimpleTimeZone Object “s1” which accepts the parameters rawOffset and the ID. The example contains 730 is the rawOffset of the ID “Australia”.

SimpleTimeZone Class Methods

MethodsDescription
Object clone ()It creates a duplicate copy of the specified instance of SimpleTimeZone.
boolean equals (Object obj)It makes comparison between two objects of SimpleTimeZone.
int getDSTsavings ()It returns the Daylight Saving Time in milliseconds, which is the time advanced during the day time.
int getOffset (int era, int year, int month, int day, int DayofWeek, int millis)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.
int getOffset (long date)It returns the offset of this TimeZone at the given time from the UTC.
int getRawOffset ()It returns the GMT offset for the specified TimeZone.
int hashcode ()For the SimpleDateFormat object, it returns the hashcode.
boolean hasSameRules (TimeZone other)It compares one TimeZone with the other TimeZone and returns true if both have the same rules and offset.
boolean inDaylightTime (Date date)It indicates whether the given date uses Daylight saving time or not. If date is set to daylight saving time, it returns boolean true value else returns boolean false value.
void setDSTSavings (int millisSavedDuringDst)Sets the amount of time in milliseconds ahead during the daylight saving time.
void setEndRule (int endMonth, int endDay, int endTime)For a fixed date of a month, it sets the daylight saving time end rule.
void setEndRule (int endMonth, int endDay, int endDayOfWeek, int endTime)It defines the end rules for the daylight saving time.
void setEndRule (int endMonth, int endDay, int endDayOfWeek, int endTime, boolean after)It defines the end rule for daylight saving time for a given fixed date of a month before or after the weekday.
void setRawOffset (int offsetMillis)The base TimeZone offset is set to the GMT. td>
void setStartRule (int startMonth, int startDay, int startTime)For a fixed date of a month, it sets the daylight saving time start rule.
void setStartRule (int startMonth, int startDay, int startDayOfWeek, int startTime)It defines the start rules for the daylight saving time.
void setStartRule (int startMonth, int startDay, int startDayOfWeek, int startTime, boolean after)It defines the start rule for daylight saving time for a given fixed date of a month before or after the weekday.
void setStartYear (int year)It sets the starting year of day light saving time.
String toString ()It is the string representation of the daylight saving time.
boolean useDaylightTime ()It indicates whether the given TimeZone uses Daylight saving time or not. If TimeZone is set to daylight saving time, it returns boolean true value else returns boolean false value.

SimpleTimeZone Class Methods Example

import Java.util. SimpleTimeZone;
public class Example_SimpleTimeZone {
	public static void main (String [] args) {
		SimpleTimeZone s1 = new SimpleTimeZone (730,"India");
		SimpleTimeZone s2 = new SimpleTimeZone (730,"Australia");

              System.out.println ("Origial s1: " + s1);

	      Object c1 = s1.clone ();
	      System.out.println ("Cloned object is: " + c1);

	      int o1=s1.getRawOffset ();

	      System.out.println ("Returns offset of TimeZone s1: " + o1);

	      System.out.println ("Checking if both have same TimeZone: " + s1.equals (s2));

	      System.out.println ("Initial value is: " + s1);

	      s1.setStartYear (2013);
	      System.out.println ("Final value is: " + s1);
	      System.out.println ("checks whether s2 Use day light time: " +s2.useDaylightTime ());

	      int d1=s1.getDSTSavings ();
	      System.out.println ("DST saving time for s1 is: " + d1);
	   }
	}
  • Above program demonstrates the few methods of SimpleTimeZone such as clone, getRawoffset, equals and useDaylightTime.
  • SimpleTimeZone s1 = new SimpleTimeZone (730,”India”); line creates the SimpleTimeZone Object “s1” which accepts the parameters, rawOffset and the ID.
  • Object c1 = s1.clone (); line creates the duplicate copy of s1, which is stored at object c1.
  • int o1=s1.getRawOffset (); line returns the RawOffset of s1, whose value is stored at o1.
  • System.out.println (“checking if both have same TimeZone: ” + s1.equals (s2)); statement checks whether two given ID’s have time zone.
  • s1.setStartYear (2013); line sets the start year for the Daylight saving time.
  • System.out.println (“checks whether s2 Use day light time: ” +s2.useDaylightTime ()); line checks whether SimpleTimeZone s2 make use of Daylight Saving Time or not.
  • int d1=s1.getDSTSavings (); line returns the Daylight Saving Time which is stored in d1.

Java SimpleTimeZone Example

Java SimpleTimeZone Example 2

 

Comments

comments

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

*