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

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

Syntax:

void java.util.SimpleTimeZone.setEndRule(int endMonth, int endDay, int endDayOfWeek, int endTime, boolean after)

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

Parameters: Five parameters are required for this method.

endMonth: The daylight saving time ending month.

endDay: The day of the month on which the daylight saving time ends.

endDayOfWeek: The daylight saving time ending day-of-week.

endTime: The daylight saving ending time in local wall clock time.

after: If true, this rule selects the first endDayOfWeek on or after endDay. If false, this rule selects the last endDayOfWeek on or before the endDay of the month.

Throws:

IllegalArgumentException - the endMonth, endDay, endDayOfWeek, or endTime parameters are out of range.

Approach 1: When no exception

Java

import java.util.SimpleTimeZone;

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

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

        int endMonth = 5, endDay = 2,
endDayOfWeek = 4, endTime = 18;

        boolean after = true;
        simpleTimeZone.setEndRule(endMonth, endDay,
endDayOfWeek, endTime, after);

        System.out.println(simpleTimeZone);
    }
}

Output:

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



Approach 2: IllegalArgumentException 

Java

import java.util.SimpleTimeZone;

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

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

        int endMonth = 15, endDay = 2,
endDayOfWeek = 4, endTime = 18;

        boolean after = true;
        simpleTimeZone.setEndRule(endMonth,
endDay, endDayOfWeek, endTime, after);

        System.out.println(simpleTimeZone);
    }
}

Output:

Exception in thread "main" java.lang.IllegalArgumentException: Illegal end month 15 at java.base/java.util.SimpleTimeZone.decodeEndRule(SimpleTimeZone.java:1431) at java.base/java.util.SimpleTimeZone.setEndRule(SimpleTimeZone.java:474) at java.base/java.util.SimpleTimeZone.setEndRule(SimpleTimeZone.java:522)


No comments:

Post a Comment