Java Utility Library

Java GregorianCalendar - add() Method



The java.util.GregorianCalendar.add() method is used to add the specified (signed) amount of time to the given calendar field, based on the calendar's rules.

Syntax

public void add(int field, int amount)

Parameters

field Specify the calendar field.
amount Specify the amount of date or time to be added to the field.

Return Value

void type.

Exception

Throws IllegalArgumentException, if field is ZONE_OFFSET, DST_OFFSET, or unknown, or if any calendar fields have out-of-range values in non-lenient mode.

Example:

In the example below, the java.util.GregorianCalendar.add() method is used to add the specified amount of time to the given calendar field.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a Calendar object with specified date
    GregorianCalendar Cal = new GregorianCalendar(2015, 11, 25);

    //printing the Calendar
    System.out.println("Original Calendar: " + Cal.getTime());

    //Adding one month in the calender
    Cal.add(GregorianCalendar.MONTH, 1);

    //printing the Calendar
    System.out.println("Modified Calendar: " + Cal.getTime());
  }
}

The output of the above code will be:

Original Calendar: Fri Dec 25 00:00:00 UTC 2015
Modified Calendar: Mon Jan 25 00:00:00 UTC 2016

❮ Java.util - GregorianCalendar