Java GregorianCalendar setWeekDate() Method



Description

The Java GregorianCalendar setWeekDate(int weekYear, int weekOfYear, int dayOfWeek) method sets this GregorianCalendar to the date given by the date specifiers - weekYear, weekOfYear, and dayOfWeek. weekOfYear follows the WEEK_OF_YEAR numbering. The dayOfWeek value must be one of the DAY_OF_WEEK values: SUNDAY to SATURDAY.

Declaration

Following is the declaration for java.util.GregorianCalendar.setGregorianChange(int weekYear, int weekOfYear, int dayOfWeek) method

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

Parameters

weekYear − the week year.

weekOfYear − the week number based on weekYear.

dayOfWeek − the day of week value: one of the constants for the DAY_OF_WEEK field: SUNDAY, ..., SATURDAY.

Return Value

This method does not return a value.

Exception

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.

Setting a Week Date in Current Dated GregorianCalendar Example

The following example shows the usage of Java GregorianCalendar setWeekDate() method. We're creating a GregorianCalendar instance of current date. We've modified calendar using setWeekDate() method and then printed the same.

package com.tutorialspoint;

import java.util.GregorianCalendar;

public class GregorianCalendarDemo {
   public static void main(String[] args) {

      // create a new calendar
      GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance();

      // print the current date and time
      System.out.println("" + cal.getTime());

      // set week date change at another date
      cal.setWeekDate(2022, 12, GregorianCalendar.SUNDAY);
      System.out.println("Date: " + cal.getTime());
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Mon Apr 29 14:05:58 IST 2024
Date: Sun Mar 13 14:05:58 IST 2022

Setting a Week Date an Year Ahead in Current Dated GregorianCalendar Example

The following example shows the usage of Java GregorianCalendar setWeekDate() method. We're creating a GregorianCalendar instance of current date. We've modified calendar using setWeekDate() method and then printed the same.

package com.tutorialspoint;

import java.util.GregorianCalendar;

public class GregorianCalendarDemo {
   public static void main(String[] args) {

      // create a new calendar
      GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance();

      // print the current date and time
      System.out.println("" + cal.getTime());

      // set week date change at another date
      cal.setWeekDate(2022, 56, GregorianCalendar.SUNDAY);
      System.out.println("Date: " + cal.getTime());
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Mon Apr 29 14:06:44 IST 2024
Date: Sun Jan 15 14:06:44 IST 2023
java_util_gregoriancalendar.htm
Advertisements