java.util.Calendar setWeekDate(int weekYear,int weekOfYear,int dayOfWeek)

Description :

This java tutorial shows how to use the setWeekDate(int weekYear,int weekOfYear,int dayOfWeek) method of Calendar class of java.util package.

This method basically sets the date of the Calendar object with the the given date specifiers as follows:

  • week year
  • week of year
  • and day of week

Contrary to the set method, all of the calendar fields and time values are calculated upon return.

Method Syntax :

public void setWeekDate(int weekYear,int weekOfYear,int dayOfWeek)

Parameter Input :

 

DataType Parameter Description
int weekYear the week year
int weekOfYear the week number based on weekYear
int dayOfWeek the day of week value

 

Method Returns :

This method returns the week year of this Calendar.

Compatibility Version :

Requires Java 1.7 and up

Exception :

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.

Java Code Example :

This java example source code demonstrates the use of setWeekDate(int weekYear,int weekOfYear,int dayOfWeek) method of Calendar class.

package com.javatutorialhq.java.examples;

import java.util.Calendar;

/*
 * This example source code demonstrates the use of  
 * setWeekDate(int weekYear,int weekOfYear,int dayOfWeek)
 *  method of Calendar class
 */

public class CalendarSetWeekDateExample {

	public static void main(String[] args){

		Calendar cal = Calendar.getInstance();
		System.out.println("Time:" + cal.getTime());		
		cal.setWeekDate(1982, 18, 5);
		System.out.println("New Time:"+cal.getTime());
		
	}
}

Sample Output :

Running the setWeekDate(int weekYear,int weekOfYear,int dayOfWeek) method example source code of Calendar class will give you the following output:

Calendar setWeekDate() method example

Calendar setWeekDate() method example

Exception Scenario :

the following sample exception is being thrown because we have put dayOfWeek as 51545 which is outside the range of allowable value for the day of the week.

Exception in thread "main" Time:Tue Feb 10 22:37:16 CST 2015
java.lang.IllegalArgumentException: invalid dayOfWeek: 51545
 at java.util.GregorianCalendar.setWeekDate(GregorianCalendar.java:2200)
 at com.javatutorialhq.java.examples.CalendarSetWeekDateExample.main(CalendarSetWeekDateExample.java:17)

Similar Method :

  • N/A

Suggested Reading List :

References :