Java Calendar - set() Method
The java.util.Calendar.set() method is used to set the values for the fields YEAR, MONTH, and DAY_OF_MONTH. Previous values of other fields are retained.
Syntax
public final void set(int year, int month, int date)
Parameters
year |
Specify the value used to set the YEAR calendar field. |
month |
Specify the value used to set the MONTH calendar field. Month value is 0-based. |
date |
Specify the value used to set the DAY_OF_MONTH calendar field. |
Return Value
void type.
Exception
NA.
Example:
In the example below, the java.util.Calendar.set() 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 1 March 2016 Cal.set(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:33:36 UTC 2021 New Calendar is: Tue Mar 01 10:33:36 UTC 2016
❮ Java.util - Calendar