java.util.Calendar set()

Description :

This java tutorial shows how to use the set() method of Calendar class of java.util package. These overloaded methods sets the value of Calendar fields.

Method Syntax :

1.

public void set(int field, int value)

2.

public final void set(int year, int month, int date)

3.

public final void set(int year, int month, int date, int hourOfDay, int minute)

4.

public final void set(int year, int month, int date, int hourOfDay, int minute, int second)

Parameter Input :

 

DataType Parameter Description
int field the given Calendar field.
int value the value to be set for the given calendar field
int year the value used to set the YEAR calendar field.
int month the value used to set the MONTH calendar field. Month value is 0-based. e.g., 0 for January.
int date the value used to set the DAY_OF_MONTH calendar field.
int hourOfDay the value used to set the HOUR_OF_DAY calendar field.
int minute the value used to set the MINUTE calendar field.
int second the value used to set the SECOND calendar field.

 

Method Returns :

This method  returns void.

Compatibility Version :

Requires Java 1.1 and up

Exception :

N/A

Discussion :

The set() method of Calendar class has 4 overloaded methods. Depending on our needs we can select which is suitable on our requirements.

The most practical to use is the first method which is set(int field, int value). This method will be your best friend in dealing with Calendar class since this method is versatile and would be able to set any calendar field base on the project requirement. From one of our example, we have used the get(int field) in getting the value of any field of the Calendar object. The field on method set(int field, int value) is the same field as required by method set(int field, int value).

The second method public final void set(int year, int month, int date), is use to set the year, month and day of the month of this Calendar object. All the rest of the field such as hour, minutes, seconds and the like remains the same as when the calendar object has been created not unless been set by another means such as setTime(Date date), setTimeInMillis(long millis), etc. If the other field values are undesired, call the clear() first.

The third method set(int year, int month, int date, int hourOfDay, int minute), is use to set the year, month, day of the month, hours, minutes of this Calendar object. All the rest of the fields emains the same as when the calendar object has been created not unless been set by another means such as setTime(Date date), setTimeInMillis(long millis), etc.

The fourth method set(int year, int month, int date, int hourOfDay, int minute, int second) is the same as the third method. Compare to the third method another parameter was added which is second. As per the method argument implies, this parameter sets the second field of this Calendar.

Its worth to make a note that the set method is the new way to set time instead of the setTime(Date date) method. You know why? Date is already deprecated.

In conclusion, depending on your requirements you can select any of the overloaded set method.

Java Code Example :

This java example source code demonstrates the use of set() method of Calendar class. Basically this example just use all the overloaded set() method and attempts to show the different result in calling these methods.

package com.javatutorialhq.java.examples;

import java.util.Calendar; import java.util.Date;

/* * This example source code demonstrates the use of * set() method of Calendar class */

public class CalendarSetExample {

public static void main(String

[] args) throws InterruptedException {

Calendar cal = Calendar.getInstance(); // Printing the current time System.out.println("Current time:" + cal.getTime());

// set the time using the 1st overloaded method cal.set(Calendar.YEAR, 2019); // set the year cal.set(Calendar.MONTH, 2); // set the month cal.set(Calendar.DAY_OF_MONTH, 23); // set the day // print the date after setting System.out.println("Time after setting:" + cal.getTime());

// clear calendar cal.clear(); System.out.println("Time after clearing:"+cal.getTime());

//set again the time using the 1st overloaded method cal.set(Calendar.YEAR, 2019); // set the year cal.set(Calendar.MONTH, 2); // set the month cal.set(Calendar.DAY_OF_MONTH, 23); // set the day System.out.println("Time after clear and set:"+cal.getTime());

// set the time using the second overloaded method cal.set(2020, 11, 14); System.out.println("Time of 2nd overloaded method:"+cal.getTime());

// set the time using the third overloaded method cal.set(2018, 6, 17, 23, 15); System.out.println("Time of 3rd overloaded method:"+cal.getTime());

//set the time using the 4th overloaded method cal.set(2007, 8, 12, 10, 12, 42); System.out.println("Time of 4th overloaded method:"+cal.getTime());

}

}

Sample Output :

Running the set() method example source code of Calendar class will give you the following output:

Calendar set() method example

Calendar set() method example

Exception Scenario :

N/A

Similar Method :

N/A

Suggested Reading List :

References :