setWeekDate(int, int, int): This method is available in java.util.Calendar class of Java.
Syntax:
void java.util.Calendar.setWeekDate(int weekYear, int weekOfYear, int dayOfWeek)
This method takes three arguments of type int as its parameters. This method sets the date of this Calendar with the given date specifiers - week year, week of the year, and day of the week.
Parameters: Three parameters are required for this method.
weekYear: the week year.
weekOfYear: the week number based on weekYear.
dayOfWeek: the day of week value.
Throws:
1. IllegalArgumentException - if any of the given date specifiers is invalid or any of the calendar fields are inconsistent with the given date specifiers in non-lenient mode.
2. UnsupportedOperationException - if any week year numbering isn't supported in this Calendar.
Approach 1: When no exception
Java
import java.util.Calendar;public class CalendarsetWeekDate {public static void main(String[] args) {// create a calendarCalendar calendar = Calendar.getInstance();int weekYear = 2022, weekOfDay = 4, dayOfWeek = 4;calendar.setWeekDate(weekYear, weekOfDay, dayOfWeek);System.out.println(calendar);}}
Output:
java.util.GregorianCalendar[time=1642614627035,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Calcutta",offset=19800000,dstSavings=0,useDaylight=false,transitions=7,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2022,MONTH=0,WEEK_OF_YEAR=4,WEEK_OF_MONTH=4,DAY_OF_MONTH=19,DAY_OF_YEAR=19,DAY_OF_WEEK=4,DAY_OF_WEEK_IN_MONTH=3,AM_PM=1,HOUR=11,HOUR_OF_DAY=23,MINUTE=20,SECOND=27,MILLISECOND=35,ZONE_OFFSET=19800000,DST_OFFSET=0]
Approach 2: IllegalArgumentException
Java
import java.util.Calendar;public class CalendarsetWeekDate {public static void main(String[] args) {// create a calendarCalendar calendar = Calendar.getInstance();int weekYear = 2022, weekOfDay = 4, dayOfWeek = 14;calendar.setWeekDate(weekYear, weekOfDay, dayOfWeek);System.out.println(calendar);}}
Output:
Exception in thread "main" java.lang.IllegalArgumentException: invalid dayOfWeek: 14 at java.base/java.util.GregorianCalendar.setWeekDate(GregorianCalendar.java:2197)
No comments:
Post a Comment