Skip to content
Related Articles

Related Articles

Improve Article

SimpleTimeZone getOffset() method in Java with Examples

  • Last Updated : 02 Jan, 2019

In SimpleTimeZone class, there are two types of getOffset() method depending upon the parameters passed to it.

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

The getOffset() method of SimpleTimeZone class is used to return the difference between local time and UTC, taking into account both the raw offset and the effect of daylight saving. This calculation is done in milliseconds.
Syntax:

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

Parameters: The function accepts 6 parameters:

  • era- specifies the era of the given date.
  • year- specifies the year in the given date.
  • month- specifies the month in the given date.
  • day- specifies the day-in-month of the given date.
  • dayOfweek- specifies the day-of-week of the given date.
  • millis- specifies the milliseconds in day in standard local time.

Return Value: It returns the amount of time in milliseconds to add to UTC to get local time.

Exception: The function throws a single exception IllegalArgumentException if the era, month, day, dayOfWeek, or millis parameters are out of range.



Program below demonstrates the above mentioned function:

Program 1:




// program to demonstrate the
// function java.util.SimpleTimeZone.getOffset()
  
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // create simple time zone object
        SimpleTimeZone obj
            = new SimpleTimeZone(520, "US");
  
        // get offset on object obj
        int off
            = obj
                  .getOffset(GregorianCalendar.AD,
                             1000,
                             10,
                             2,
                             4,
                             6000);
  
        // print offset value
        System.out.println("Offset = "
                           + off);
    }
}
Output:
Offset = 520

Program 2:




// program to demonstrate the
// function java.util.SimpleTimeZone.getOffset()
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // create simple time zone object
        SimpleTimeZone obj
            = new SimpleTimeZone(780, "US");
  
        // get offset on object obj
        int off
            = obj
                  .getOffset(GregorianCalendar.AD,
                             1000,
                             10,
                             2,
                             4,
                             6000);
  
        // print offset value
        System.out.println("Offset = "
                           + off);
    }
}
Output:
Offset = 780

getOffset(long date)

The getOffset() method of SimpleTimeZone class is used to return the offset of this time zone from UTC at the given time.

Syntax:

public int getOffset(long date)

Parameters: The function accepts a single parameter date which specifies the time at which the time zone offset is found.



Return Value: It returns the amount of time in milliseconds to add to UTC to get local time.

Exception: The function does not throws any exception.

Program below demonstrates the above mentioned function:

Program 1:




// program to demonstrate the
// function java.util.SimpleTimeZone.getOffset()
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // create simple time zone object
        SimpleTimeZone obj
            = new SimpleTimeZone(520, "India");
  
        // get offset on object obj
        int off
            = obj
                  .getOffset(Calendar.ZONE_OFFSET);
  
        // print offset value
        System.out.println("Offset = "
                           + off);
    }
}
Output:
Offset = 520

Program 2:




// program to demonstrate the
// function java.util.SimpleTimeZone.getOffset()
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // create simple time zone object
        SimpleTimeZone obj
            = new SimpleTimeZone(600, "India");
  
        // get offset on object obj
        int off
            = obj
                  .getOffset(Calendar.ZONE_OFFSET);
  
        // print offset value
        System.out.println("Offset = "
                           + off);
    }
}
Output:
Offset = 600

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :