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
java_util_gregoriancalendar.htm
Advertisements