Java Date Time - Java GregorianCalendar.add(int field, int amount)








Syntax

GregorianCalendar.add(int field, int amount) has the following syntax.

public void add(int field, int amount)

Example

In the following code shows how to use GregorianCalendar.add(int field, int amount) method.

/*from w w w .  j  ava2 s  . c o  m*/
import java.util.*;

public class Main {

   public static void main(String[] args) {

      GregorianCalendar cal = 
              (GregorianCalendar) GregorianCalendar.getInstance();

      // add 2 months 
      cal.add((GregorianCalendar.MONTH), 2);

      // print the modified date and time
      System.out.println(cal.getTime());

      // add 2 years 
      cal.add((GregorianCalendar.YEAR), 2);

      // print the modified date and time
      System.out.println(cal.getTime());

   }
}

The code above generates the following result.