Java Utility Library

Java GregorianCalendar - setWeekDate() Method



The java.util.GregorianCalendar.setWeekDate() method is used to set this GregorianCalendar to the date given by the date specifiers - weekYear, weekOfYear, and dayOfWeek.

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 if any of the calendar fields are inconsistent with the given date specifiers in non-lenient mode.

Example:

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

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a Calendar object with specified date
    GregorianCalendar Cal = new GregorianCalendar(2015, 10, 25);

    //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: Wed Nov 25 00:00:00 UTC 2015
New Calendar is: Sun Jan 03 00:00:00 UTC 2016

❮ Java.util - GregorianCalendar