Java.util.Calendar.roll() Method


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


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

public abstract void roll(int field,boolean up)


  • field − The field to be altered.

  • up − Indicates if the value of the specified time field is to be increased or decreased.

Return Value

This method does not return a value.




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

package com.tutorialspoint;

import java.util.*;

public class CalendarDemo {
   public static void main(String[] args) {

      // create a calendar
      Calendar cal = Calendar.getInstance();

      // displays the current calendar
      System.out.println("Month is " + cal.get(Calendar.MONTH));

      // roll month
      cal.roll(Calendar.MONTH, true);

      // print result after rolling
      System.out.println("Month is " + cal.get(Calendar.MONTH));

      // roll downwards
      cal.roll(Calendar.MONTH, false);

      // print result after rolling down
      System.out.println("Month is " + cal.get(Calendar.MONTH));

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

Month is 4
Month is 5
Month is 4
Kickstart Your Career

Get certified by completing the course

Get Started