Java Utility Library

Java Calendar - setWeekDate() Method



The java.util.Calendar.setWeekDate() method is used to set the date of this Calendar with the given date specifiers - week year, week of year, and day of week.

Syntax

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

Parameters

weekYear Specify the week year.
weekOfYear Specify the week number based on weekYear.
dayOfWeek Specify the day of week value.

Return Value

void type.

Exception

  • Throws 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.
  • Throws UnsupportedOperationException, if any week year numbering isn't supported in this Calendar.

Example:

In the example below, the java.util.Calendar.setWeekDate() method is used to set the given Calendar to the specified date.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a Calendar instance
    Calendar Cal = Calendar.getInstance();

    //printing the Calendar
    System.out.println("The Calendar is: " + Cal.getTime());

    //setting the calendar to Sunday of 2nd Week of 2016
    Cal.setWeekDate(2016, 2, 1);

    //printing the Calendar
    System.out.println("New Calendar is: " + Cal.getTime());
  }
}

The output of the above code will be:

The Calendar is: Sat May 08 10:25:42 UTC 2021
New Calendar is: Sun Jan 03 10:25:42 UTC 2016

❮ Java.util - Calendar