Java.util.Calendar.roll() Method



Description

The java.util.Calendar.roll(int field,int amount) method adds the specified amount to the specified calendar field without changing larger fields. Amount is signed (negative means reduce).

Declaration

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

public void roll(int field,int amount)

Parameters

  • field − The field to be altered.

  • amount − The signed amount to add to the calendar field.

Return Value

This method does not return a value.

Exception

NA

Example

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();

      // 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));
   }
}

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

Month is 4
Month is 6
Month is 2
java_util_calendar.htm
Advertisements