SimpleTimeZone getOffset(int, int, int, int, int, int) in Java

getOffset(int, int, int, int, int, int): This method is available in java.util.SimpleTimeZone class of Java.

Syntax:

int java.util.SimpleTimeZone.getOffset(int era, int year, int month, int day, int dayOfWeek, int millis)

This method takes six arguments. 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.

Parameters: Six parameters are required for this method.

era: The era of the given date.

year: The year in the given date.

month: The month in the given date.

day: The day-in-month of the given date.

dayOfWeek: The day-of-week of the given date.

millis: The milliseconds in day in standard local time.

Returns: The milliseconds to add to UTC to get local time.

Throws:

IllegalArgumentException - the era, month, day, dayOfWeek,or millis parameters are out of range

Approach 1: When no exception

Java

import java.util.SimpleTimeZone;

public class SimpleTimeZonegetOffset2 {
    public static void main(String[] args) {

        SimpleTimeZone simpleTimeZone =
new SimpleTimeZone(100, "India");

        int era = 1, year = 2022, month = 3,
day = 29, dayOfWeek = 4, millis = 1910;

        System.out.println(simpleTimeZone.getOffset(era,
year, month, day, dayOfWeek, millis));
    }
}

Output:

100


Approach 2: IllegalArgumentException 

Java

import java.util.SimpleTimeZone;

public class SimpleTimeZonegetOffset2 {
    public static void main(String[] args) {

        SimpleTimeZone simpleTimeZone =
new SimpleTimeZone(100, "India");

        int era = -1, year = 2022, month = 3,
day = 29, dayOfWeek = 4, millis = 1910;

        System.out.println(simpleTimeZone.getOffset(era,
year, month, day, dayOfWeek, millis));
    }
}

Output:

Exception in thread "main" java.lang.IllegalArgumentException: Illegal era -1 at java.base/java.util.SimpleTimeZone.getOffset(SimpleTimeZone.java:608)


No comments:

Post a Comment