SimpleTimeZone getOffset() method in Java with Examples

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

Offset = 520

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

Offset = 520

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

Offset = 600


My Personal Notes arrow_drop_up

Contented with little yet wishing for more

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.