Java.util.GregorianCalendar.roll() Method



The java.util.GregorianCalendar.roll(int field,boolean up) method adds or subtracts (up/down) a single unit of time on the given time field without changing larger fields.


Following is the declaration for java.util.GregorianCalendar.roll() method

public void roll(int field,boolean up)


  • up -- indicates if the value of the specified calendar field is to be rolled up or rolled down. Use true if rolling up, false otherwise.

  • field -- the time field.

Return Value

This method does not return a value.


  • IllegalArgumentException -- if field is ZONE_OFFSET, DST_OFFSET, or unknown, or if any calendar fields have out-of-range values in non-lenient mode.


The following example shows the usage of java.util.GregorianCalendar.roll() method.

package com.tutorialspoint;

import java.util.*;

public class GregorianCalendarDemo {

   public static void main(String[] args) {

   // create a new calendar
   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());


Let us compile and run the above program, this will produce the following result:

Fri May 18 13:31:57 EEST 2012
Date:Mon Jun 18 13:31:57 EEST 2012
Date:Sat Jun 18 13:31:57 EEST 2011