Calendar.set()

Date

Calendar.set(field, int value) Assign the values to fields
field: Required fields of Calendar like YEAR, MONTH, DAY_OF_MONTH, HOUR, MINUTE, and SECOND
value: Required , the value to be set for the given field.

Examples with output
Let us create one calendar object and we update a new Year value to it.
package my_proj;
 
import java.util.Calendar;
 
public class my_first {
public static void main(String[] args) {

Calendar my_cal = Calendar.getInstance();// created calendar 

System.out.println(my_cal.getTime());//
my_cal.set(Calendar.YEAR,2025); // Update the year
System.out.println(my_cal.getTime());//
}
}
Output is here
Tue Mar 03 15:47:11 IST 2020
Mon Mar 03 15:47:11 IST 2025
Adding year, month , date , hour , minutes, seconds
Calendar my_cal_d1 = Calendar.getInstance();// created calendar 
my_cal_d1.set(2020,2,25,10,10,20);
System.out.println( my_cal_d1.getTime());
Output is here
Wed Mar 25 10:10:20 IST 2020

Updating other field values

We will update all other fields
package my_proj;
 
import java.util.Calendar;
 
public class my_first {
public static void main(String[] args) {

Calendar my_cal = Calendar.getInstance();// created calendar 

System.out.println(my_cal.getTime());//
my_cal.set(Calendar.YEAR,2025); 
my_cal.set(Calendar.MONTH,8); 
my_cal.set(Calendar.DAY_OF_MONTH,26);
my_cal.set(Calendar.HOUR,22);
my_cal.set(Calendar.MINUTE,55);
my_cal.set(Calendar.SECOND,52);
System.out.println(my_cal.getTime());

}
}
Output is here
Tue Mar 03 16:24:02 IST 2020
Sat Sep 27 10:55:52 IST 2025

Using clear()

We can reset all field values by using clear(). Add this code before the last line.
my_cal.clear();
System.out.println(my_cal.getTime());
Output ( Complete ) is here
Tue Mar 03 16:30:06 IST 2020
Thu Jan 01 00:00:00 IST 1970
clear() Date & time tutorials



plus2net.com



Post your comments , suggestion , error , requirements etc here




We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2020 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer