- Java.util Package Classes
- Java.util - Home
- Java.util - ArrayDeque
- Java.util - ArrayList
- Java.util - Arrays
- Java.util - BitSet
- Java.util - Calendar
- Java.util - Collections
- Java.util - Currency
- Java.util - Date
- Java.util - Dictionary
- Java.util - EnumMap
- Java.util - EnumSet
- Java.util - Formatter
- Java.util - GregorianCalendar
- Java.util - HashMap
- Java.util - HashSet
- Java.util - Hashtable
- Java.util - IdentityHashMap
- Java.util - LinkedHashMap
- Java.util - LinkedHashSet
- Java.util - LinkedList
- Java.util - ListResourceBundle
- Java.util - Locale
- Java.util - Observable
- Java.util - PriorityQueue
- Java.util - Properties
- Java.util - PropertyPermission
- Java.util - PropertyResourceBundle
- Java.util - Random
- Java.util - ResourceBundle
- Java.util - ResourceBundle.Control
- Java.util - Scanner
- Java.util - ServiceLoader
- Java.util - SimpleTimeZone
- Java.util - Stack
- Java.util - StringTokenizer
- Java.util - Timer
- Java.util - TimerTask
- Java.util - TimeZone
- Java.util - TreeMap
- Java.util - TreeSet
- Java.util - UUID
- Java.util - Vector
- Java.util - WeakHashMap
- Java.util Package Extras
- Java.util - Interfaces
- Java.util - Exceptions
- Java.util - Enumerations
- Java.util Useful Resources
- Java.util - Useful Resources
- Java.util - Discussion
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
To Continue Learning Please Login
Login with Google