java.util.Calendar roll(int field, boolean up)

Description :

This java tutorial shows how to use the roll(int field, boolean up) method of Calendar class of java.util package. This method adds or subtracts (up/down) a single unit of time on the given time field without changing larger fields.

Method Syntax :

public abstract void roll(int field, boolean up)

Parameter Input :

 

DataType Parameter Description
int field the time field
boolean up indicates if the value of the specified time field is to be rolled up or rolled down. Use true if rolling up, false otherwise.

 

Method Returns :

This method returns void.

Compatibility Version :

Requires Java 1.1 and up

Exception :

N/A

Java Code Example :

This java example source code demonstrates the use of roll(int field, boolean up)  method of Calendar class. Basically on this example we just add and subtract values on different Calendar fields.

package com.javatutorialhq.java.examples;

import java.util.Calendar;

/*
 * This example source code demonstrates the use of  
 * roll(int field, boolean up) method of Calendar class
 */

public class CalendarRollExample {

	public static void main(String[] args) {

		Calendar cal = Calendar.getInstance();
		System.out.println("Time:" + cal.getTime());
		cal.roll(Calendar.YEAR, false);
		System.out.println("Time rolling down the year:" + cal.getTime());
		cal.roll(Calendar.HOUR, true);
		System.out.println("Time rolling up the hour:" + cal.getTime());

	}
}

Sample Output :

Running the roll(int field, boolean up) method example source code of Calendar class will give you the following output:

Time:Sun Feb 08 02:32:41 CST 2015
Time rolling down the year:Sat Feb 08 02:32:41 CST 2014
Time rolling up the hour:Sat Feb 08 03:32:41 CST 2014

Exception Scenario :

N/A

Similar Method :

  • N/A

Suggested Reading List :

References :