Java Calendar roll() Method



Description

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

Declaration

Following is the declaration for java.util.Calendar.roll(int field,boolean up) method

public abstract void roll(int field,boolean up)

Parameters

  • 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.

Exception

NA

Java Calendar roll(int field,int amount) Method

Description

The Java 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(int field,int amount) 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

Rolling Month in a Current Dated Calendar Instance Example

The following example shows the usage of Java Calendar roll(field,up) method. We're creating an instance of a Calendar of current date using getInstance() method and print the current month value and then roll it upwards, print the updated result and then roll it downwards and print the result.

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

Output

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

Month is 8
Month is 9
Month is 8

Rolling Month in a Current Dated GregorianCalendar Instance Example

The following example shows the usage of Java Calendar roll(field,amount) method. We're creating an instance of a Calendar of current date using GregorianCalendar and print the current month value and then roll it by 2, print the updated result and then roll it by -2 and print the result.

package com.tutorialspoint;

import java.util.Calendar;
import java.util.GregorianCalendar;

public class CalendarDemo {
   public static void main(String[] args) {
   
      // create a calendar    
      Calendar cal = new GregorianCalendar();

      // displays 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, -2);

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

Output

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

Month is 8
Month is 10
Month is 8

Rolling Month in a Given Dated GregorianCalendar Instance Example

The following example shows the usage of Java Calendar roll(field,up) method. We're creating an instance of a Calendar of particular date using GregorianCalendar and print the current month value and then roll it upwards, print the updated result and then roll it downwards and print the result.

package com.tutorialspoint;

import java.util.Calendar;
import java.util.GregorianCalendar;

public class CalendarDemo {
   public static void main(String[] args) {
   
      // create a calendar    
      Calendar cal = new GregorianCalendar(2025,8,26);

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

Output

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

Month is 8
Month is 9
Month is 8
java_util_calendar.htm
Advertisements