Open In App

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:

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

Article Tags :