SimpleTimeZone setStartRule(int, int, int, int, boolean) in Java

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

Syntax:

void java.util.SimpleTimeZone.setStartRule(int startMonth, int startDay, int startDayOfWeek, int startTime, boolean after)

This method takes five arguments. This method sets the daylight saving time start rule to a weekday before or after the given date within a month.

Parameters: Five parameters are required for this method.

startMonth: The daylight saving time starting month.

startDay: The day of the month on which the daylight saving time starts.

startDayOfWeek: The daylight saving time starting day of week.

startTime: The daylight saving time starting time in local wall clock time.

after: If true, this rule selects the first dayOfWeek on or after dayOfMonth. If false, this rule selects the last dayOfWeek on or before dayOfMonth.

Returns: NA

Throws:

IllegalArgumentException - if the startMonth, startDay, startDayOfWeek, or startTime parameters are out of range.

Approach 1: When no exception

Java

import java.util.SimpleTimeZone;

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

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

        int startMonth = 1, startDay = 1,
startDayOfWeek = 1, startTime = 0;
        boolean after = true;
        simpleTimeZone.setStartRule(startMonth,
startDay, startDayOfWeek, startTime, after);

        System.out.println(simpleTimeZone);
    }
}

Output:

java.util.SimpleTimeZone[id=India,offset=100,dstSavings=3600000,useDaylight=false,startYear=0,startMode=3,startMonth=1,startDay=1,startDayOfWeek=1,startTime=0,startTimeMode=0,endMode=0,endMonth=0,endDay=0,endDayOfWeek=0,endTime=0,endTimeMode=0]



Approach 2: IllegalArgumentException

Java

import java.util.SimpleTimeZone;

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

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

        int startMonth = 16, startDay = 1,
startDayOfWeek = 1, startTime = 0;
        boolean after = true;
        simpleTimeZone.setStartRule(startMonth,
startDay, startDayOfWeek, startTime, after);

        System.out.println(simpleTimeZone);
    }
}

Output:

Exception in thread "main" java.lang.IllegalArgumentException: Illegal start month 16 at java.base/java.util.SimpleTimeZone.decodeStartRule(SimpleTimeZone.java:1384) at java.base/java.util.SimpleTimeZone.setStartRule(SimpleTimeZone.java:393) at java.base/java.util.SimpleTimeZone.setStartRule(SimpleTimeZone.java:441)


No comments:

Post a Comment