Wednesday, July 3, 2024
HomeLanguagesJavaSimpleTimeZone getOffset() method in Java with Examples

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:




// 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

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments