setWeekDate(int, int, int): This method is available in java.util.GregorianCalendar class of Java.
Syntax:
void java.util.GregorianCalendar.setWeekDate(int weekYear, int weekOfYear, int dayOfWeek)
This method takes three arguments of type int as its parameters. This method sets this GregorianCalendar to the date given by the date specifiers - weekYear, weekOfYear, and dayOfWeek.
Parameters: Three parameters are required for this method.
weekYear: the week year.
weekOfYear: the week number based on weekYear.
dayOfWeek: the day of the week.
Throws:
IllegalArgumentException - if any of the given date specifiers is invalid, or if any of the calendar fields are inconsistent with the given date specifiers in non-lenient mode
Approach 1: When no exception
Java
import java.util.GregorianCalendar;public class GregorianCalendarsetWeekDate {public static void main(String[] args) {GregorianCalendar cal = new GregorianCalendar();int weekYear = 2022, weekOfYear = 5, dayOfWeek = 5;cal.setWeekDate(weekYear, weekOfYear, dayOfWeek);System.out.println(cal);}}
Output:
java.util.GregorianCalendar[time=1643298314768,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=5,WEEK_OF_MONTH=5,DAY_OF_MONTH=27,DAY_OF_YEAR=27,DAY_OF_WEEK=5,DAY_OF_WEEK_IN_MONTH=4,AM_PM=1,HOUR=9,HOUR_OF_DAY=21,MINUTE=15,SECOND=14,MILLISECOND=768,ZONE_OFFSET=19800000,DST_OFFSET=0]
Approach 2: IllegalArgumentException
Java
import java.util.GregorianCalendar;public class GregorianCalendarsetWeekDate {public static void main(String[] args) {GregorianCalendar cal = new GregorianCalendar();int weekYear = 2022, weekOfYear = 5, dayOfWeek = 25;cal.setWeekDate(weekYear, weekOfYear, dayOfWeek);System.out.println(cal);}}
Output:
Exception in thread "main" java.lang.IllegalArgumentException: invalid dayOfWeek: 25 at java.base/java.util.GregorianCalendar.setWeekDate(GregorianCalendar.java:2197)
No comments:
Post a Comment