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








Syntax

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

public void roll(int field, boolean up)

Example

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

/* www. j  a  v  a 2 s  .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 a month
      cal.roll(GregorianCalendar.MONTH, true);
      System.out.println("Date:" + cal.getTime());

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

   }
}

The code above generates the following result.