- 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 GregorianCalendar roll(int field,boolean up) Method
Description
The Java GregorianCalendar roll(int field,boolean up) method adds or subtracts (up/down) a single unit of time on the given time field without changing larger fields.
Declaration
Following is the declaration for java.util.GregorianCalendar.roll() method
public void roll(int field,boolean up)
Parameters
up − indicates if the value of the specified calendar field is to be rolled up or rolled down. Use true if rolling up, false otherwise.
field − the time field.
Return Value
This method does not return a value.
Exception
IllegalArgumentException − if field is ZONE_OFFSET, DST_OFFSET, or unknown, or if any calendar fields have out-of-range values in non-lenient mode.
Java GregorianCalendar roll(int field,int amount) Method
Description
The Java GregorianCalendar roll(int field,int amount) method adds a signed amount to the specified calendar field without changing larger fields. A negative roll amount means to subtract from field without changing larger fields. If the specified amount is 0, this method performs nothing.
Declaration
Following is the declaration for java.util.GregorianCalendar.roll() method
public void roll(int field,int amount)
Parameters
field − the calendar field.
amount − the signed amount to add to field.
Return Value
This method does not return a value.
Exception
IllegalArgumentException − if field is ZONE_OFFSET, DST_OFFSET, or unknown, or if any calendar fields have out-of-range values in non-lenient mode.
Rolling a Month of Current Dated GregorianCalendar Instance Example
The following example shows the usage of Java GregorianCalendar roll(int, boolean) method. We're creating a GregorianCalendar instance of current date. We're rolled a date by a month and printed it.
package com.tutorialspoint; import java.util.GregorianCalendar; public class GregorianCalendarDemo { public static void main(String[] args) { // create a new calendar GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance(); // print the current date and time System.out.println("" + cal.getTime()); // roll a month cal.roll(GregorianCalendar.MONTH, true); System.out.println("Date:" + cal.getTime()); } }
Output
Let us compile and run the above program, this will produce the following result −
Sat Nov 19 16:02:58 IST 2022 Date:Mon Dec 19 16:02:58 IST 2022
Rolling a Year Backwards of Current Dated GregorianCalendar Instance Example
The following example shows the usage of Java GregorianCalendar roll(int, boolean) method. We're creating a GregorianCalendar instance of current date. We're rolled a date backwards by an year and printed it.
package com.tutorialspoint; import java.util.GregorianCalendar; public class GregorianCalendarDemo { public static void main(String[] args) { // create a new calendar GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance(); // print the current date and time System.out.println("" + cal.getTime()); // roll a year backwards cal.roll(GregorianCalendar.YEAR, false); System.out.println("Date:" + cal.getTime()); } }
Output
Let us compile and run the above program, this will produce the following result −
Sat Nov 19 16:09:01 IST 2022 Date:Fri Nov 19 16:09:01 IST 2021
Rolling Years of Current Dated GregorianCalendar Instance Example
The following example shows the usage of Java GregorianCalendar roll(int, int) method. We're creating a GregorianCalendar instance of current date. We're rolled a date by three years and printed it.
package com.tutorialspoint; import java.util.GregorianCalendar; public class GregorianCalendarDemo { public static void main(String[] args) { // create a new calendar GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance(); // print the current date and time System.out.println("" + cal.getTime()); // roll a year cal.roll(GregorianCalendar.YEAR, 3); System.out.println("Date:" + cal.getTime()); } }
Output
Let us compile and run the above program, this will produce the following result −
Sat Nov 19 16:09:57 IST 2022 Date:Wed Nov 19 16:09:57 IST 2025
To Continue Learning Please Login
Login with Google