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








Syntax

Calendar.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 Calendar.roll(int field, int amount) method.

//  ww  w  . j  a  va2  s . co  m

import java.util.Calendar;

public class Main {

   public static void main(String[] args) {

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

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

      // print result after rolling
      System.out.println("Month is " + cal.get(Calendar.MONTH));
      
      // roll downwards
      cal.roll(Calendar.MONTH, -4);
      
      // print result
      System.out.println("Month is " + cal.get(Calendar.MONTH));
   }
}

The code above generates the following result.