GregorianCalendar class add(int field, int amount) method example
Adds the specified (signed) amount of time to the given calendar field
GregorianCalendarclass add(int field, int amount) method example. This example shows you how to use add(int field, int amount) method.This method Adds the specified (signed) amount of time to the given calendar field, based on the calendar's rules.
Here is the code:-
/**
* @Program Adds the specified (signed) amount of time to the given calendar
* field, based on the calendar's rules.
* add.java
* Author:-RoseIndia Team
* Date:-27-May-2008
*/
import java.util.*;
public class add {
public static void main(String[] args) {
//Creating a Calendar
GregorianCalendar g=new GregorianCalendar();
//Displaying the Current year of the calendar
System.out.println("Current Year is: "+g.get(Calendar.YEAR));
// Add 5 years to the current calendar:
g.add(1,5);
System.out.println("Year after adding 5years to currentyear is: "+g.get(Calendar.YEAR));
}
} |
Output of the program:-
Current Year is: 2008
Year after adding 5years to currentyear is: 2013 |
|