Calendar class add(int field, int amount) method example
Calendar class add(int field, int amount) method example. This example shows you how to use add(int field, int amount) method.This method Adds or subtracts the specified amount of time to the given calendar field, based on the calendar's rules.
Here is the code:-
/**
* @Program that Adds or subtracts the specified amount of time to the given
* calendar field, based on the calendar's rules.
* addCalender.java
* Author:-RoseIndia Team
* Date:-21-May-2008
*/
import java.util.*;
public class addCalender {
public static void main(String[] args) {
Calendar cal = new GregorianCalendar();
//displays current date and time
System.out.println("Today : " + cal.getTime());
// Substract 30 days from the calendar
cal.add(Calendar.DATE, -30);
System.out.println("30 days ago: " + cal.getTime());
// Add 10 months to the calendar
cal.add(Calendar.MONTH, 10);
System.out.println("10 months later: " + cal.getTime());
}
} |
Output of the program:-
Today : Thu May 22 17:24:11 IST 2008
30 days ago: Tue Apr 22 17:24:11 IST 2008
10 months later: Sun Feb 22 17:24:11 IST 2009 |
|