Java SimpleTimeZone Class



Introduction

The Java SimpleTimeZone class is a concrete subclass of TimeZone that represents a time zone for use with a Gregorian calendar. Following are the important points about SimpleTimeZone −

  • The class holds an offset from GMT, called raw offset.

  • This class also holds start and end rules for a daylight saving time schedule.

Class declaration

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

public class SimpleTimeZone
   extends TimeZone

Field

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

  • static int STANDARD_TIME − This is the constant for a mode of start or end time specified as standard time.

  • static int UTC_TIME − This is the constant for a mode of start or end time specified as UTC.

  • static int WALL_TIME − This is the constant for a mode of start or end time specified as wall clock time.

It also consists of fields inherited from class TimeZone.

Class constructors

Sr.No. Constructor & Description
1

SimpleTimeZone(int rawOffset, String ID)

This constructs a SimpleTimeZone with the given base time zone offset from GMT and time zone ID with no daylight saving time schedule.

2

SimpleTimeZone(int rawOffset, String ID, int startMonth, int startDay, int startDayOfWeek, int startTime, int endMonth, int endDay, int endDayOfWeek, int endTime)

This constructs a SimpleTimeZone with the given base time zone offset from GMT, time zone ID, and rules for starting and ending the daylight time.

3

SimpleTimeZone(int rawOffset, String ID, int startMonth, int startDay, int startDayOfWeek, int startTime, int endMonth, int endDay, int endDayOfWeek, int endTime, int dstSavings)

This constructs a SimpleTimeZone with the given base time zone offset from GMT, time zone ID, and rules for starting and ending the daylight time.

4

SimpleTimeZone(int rawOffset, String ID, int startMonth, int startDay, int startDayOfWeek, int startTime, int startTimeMode, int endMonth, int endDay, int endDayOfWeek, int endTime, int endTimeMode, int dstSavings)

This constructs a SimpleTimeZone with the given base time zone offset from GMT, time zone ID, and rules for starting and ending the daylight time.

Class methods

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

This method returns a clone of this SimpleTimeZone instance.

2 boolean equals(Object obj)

This method compares the equality of two SimpleTimeZone objects.

3 int getDSTSavings()

This method returns the amount of time in milliseconds that the clock is advanced during daylight saving time.

4 int getOffset(int era, int year, int month, int day, int dayOfWeek, int millis)

This method returns the difference in milliseconds between local time and UTC, taking into account both the raw offset and the effect of daylight saving, for the specified date and time.

5 int getRawOffset()

This method gets the GMT offset for this time zone.

6 int hashCode()

This method generates the hash code for the SimpleDateFormat object.

7 boolean hasSameRules(TimeZone other)

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

8 boolean inDaylightTime(Date date)

This method queries if the given date is in daylight saving time.

9 boolean observesDaylightTime()

This method queries if the given date is in daylight saving time.

10 void setDSTSavings(int millisSavedDuringDST)

This method sets the amount of time in milliseconds that the clock is advanced during daylight saving time.

11 void setEndRule(int endMonth, int endDay, int endTime)

This method sets the daylight saving time end rule to a fixed date within a month.

12 void setRawOffset(int offsetMillis)

This method sets the base time zone offset to GMT.

13 void setStartRule(int startMonth, int startDay, int startTime)

This method Sets the daylight saving time start rule to a fixed date within a month.

14 void setStartYear​(int year)

This method sets the daylight saving time starting year.

15 String toString()

This method returns a string representation of this time zone.

16 boolean useDaylightTime()

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

Methods inherited

This class inherits methods from the following classes −

  • java.util.TimeZone
  • java.util.Object

Getting the Difference Of Time During Daylight Savings Time Example

The following example shows the usage of Java SimpleTimeZone getDSTSavings() method to get the difference of time during daylight savings time. We've created a SimpleTimeZone using US then printed the savings time difference.

package com.tutorialspoint;

import java.util.SimpleTimeZone;

public class SimpleTimeZoneDemo {
   public static void main( String args[] ) {
      
      // create simple time zone object 
      SimpleTimeZone stobj = new SimpleTimeZone(720,"US");

      // check DST saving
      System.out.println("DST saving : " + stobj.getDSTSavings());
   }    
}

Output

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

DST saving : 0
Advertisements