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








Syntax

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

public void roll(int field, int amount)

Example

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

/* w ww.  j  av  a 2s .c o  m*/

import java.util.*;

public class Main {

   public static void main(String[] args) {

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

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

      // roll 2 months
      cal.roll(GregorianCalendar.MONTH, 2);
      System.out.println("Date:" + cal.getTime());

      // roll 5 year backwards
      cal.roll(GregorianCalendar.YEAR, -5);
      System.out.println("Date:" + cal.getTime());

   }
}

The code above generates the following result.