Calendar add() Method in Java with Examples

The add(int field, int amt) method of Calendar class is used to add or subtract from the given calendar field(int field), a specific amount of time(int amt), based on the calendar’s rules.

Syntax:

public abstract void add(int field, int amt)

Parameters: The method takes two parameters:

  • field: This is of integer type and refers to the field of the calendar on which the operation is to be performed.
  • amt: This is of integer type and refers to the amount of time needed to be subtracted.

Return Value: The method does not return any value.

Below programs illustrate the working of add() Method of Calendar class:
Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Code to illustrate add() Method
  
import java.util.Calendar;
  
public class CalendarClassDemo {
    public static void main(String args[])
    {
        // Creating a calendar object
        Calendar calndr
            = Calendar.getInstance();
  
        // Displaying the Current Date
        System.out.println("Current Date: "
                           + calndr.getTime());
  
        // Adding 50 days to the
        // Current Calendar
        calndr.add(Calendar.DATE, 50);
        System.out.println("After 50 days: "
                           + calndr.getTime());
  
        // Subtracting 6 months from
        // the Current calendar
        calndr.add(Calendar.MONTH, -6);
        System.out.println("6 months ago it was: "
                           + calndr.getTime());
  
        // Subtracting 2 year from
        // the Current calendar
        calndr.add(Calendar.YEAR, -2);
        System.out.println("2 years ago it was: "
                           + calndr.getTime());
    }
}

chevron_right


Output:

Current Date: Tue Feb 12 10:54:21 UTC 2019
After 50 days: Wed Apr 03 10:54:21 UTC 2019
6 months ago it was: Wed Oct 03 10:54:21 UTC 2018
2 years ago it was: Mon Oct 03 10:54:21 UTC 2016

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Code to illustrate add() Method
  
import java.util.Calendar;
  
public class CalendarClassDemo {
    public static void main(String args[])
    {
        // Creating a calendar object
        Calendar calndr
            = Calendar.getInstance();
  
        // Displaying the Current Date
        System.out.println("Current Date: "
                           + calndr.getTime());
  
        // Adding 30 days to the
        // Current Calendar
        calndr.add(Calendar.DATE, 30);
        System.out.println("After 30 days: "
                           + calndr.getTime());
  
        // Subtracting 3 months from
        // the Current calendar
        calndr.add(Calendar.MONTH, -3);
        System.out.println("3 months ago it was: "
                           + calndr.getTime());
  
        // Subtracting 10 years from
        // the Current calendar
        calndr.add(Calendar.YEAR, -10);
        System.out.println("10 years ago it was: "
                           + calndr.getTime());
    }
}

chevron_right


Output:

Current Date: Tue Feb 12 10:54:24 UTC 2019
After 30 days: Thu Mar 14 10:54:24 UTC 2019
3 months ago it was: Fri Dec 14 10:54:24 UTC 2018
10 years ago it was: Sun Dec 14 10:54:24 UTC 2008

Reference: https://docs.oracle.com/javase/9/docs/api/java/util/Calendar.html#add-int-int-



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.