Java.util.Calendar.set() Method
Description
The java.util.Calendar.set(int year, int month, int date) method sets the values for the calendar fields YEAR, MONTH, and DAY_OF_MONTH.
Declaration
Following is the declaration for java.util.Calendar.set() method
public final void set(int year,int month,int date)
Parameters
year − Value to be used for YEAR field
month − Value to be used for MONTH field. 0 is January
day − Value to be used for DAY field
Return Value
This method does not return a value.
Exception
NA
Example
The following example shows the usage of java.util.calendar.set() method.
package com.tutorialspoint;
import java.util.Calendar;
public class CalendarDemo {
public static void main(String[] args) {
// create a calendar
Calendar cal = Calendar.getInstance();
// print current time
System.out.println("Current year is :" + cal.getTime());
// set the year,month and day to something else
cal.set(1995, 5, 25);
// print the result
System.out.println("Altered year is :" + cal.getTime());
}
}
Let us compile and run the above program, this will produce the following result −
Current year is :Wed May 02 19:09:29 EEST 2012 Altered year is :Sun Jun 25 19:09:29 EEST 1995
java_util_calendar.htm
Advertisements