Calendar add() Method in Java with Examples - GeeksforGeeks

02 Nov, 2021
The add() method of Calendar class present inside 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: 
The field of the calendar on which the operation is to be performed. (Integer type)
The amount of time needed to be subtracted. (Integer type)
Return Value: The method does not return any value.
Example 1:
Java
// Java Program to Illustrate add() Method// of Calendar class // Importing required classesimport java.util.Calendar; // Main classpublic class CalendarClassDemo {     // Main driver method    public static void main(String args[])    {        // Creating a Calendar class object        Calendar calndr = Calendar.getInstance();         // Displaying the current date        // using getTime() method        System.out.println("Current Date: "                           + calndr.getTime());         // Adding 50 days to the        // Current Calendar        // using add() method        calndr.add(Calendar.DATE, 50);         // Displaying the date now using getTime() method        System.out.println("After 50 days: "                           + calndr.getTime());         // Subtracting 6 months from        // the Current calendar        calndr.add(Calendar.MONTH, -6);         // Displaying the date now using getTime() method        System.out.println("6 months ago it was: "                           + calndr.getTime());         // Subtracting 2 year from        // the Current calendar        calndr.add(Calendar.YEAR, -2);         // Displaying the date now using getTime() method        System.out.println("2 years ago it was: "                           + calndr.getTime());    }}
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:
Java
// Java Program to Illustrate add() Method// of Calendar class // Importing required classesimport java.util.Calendar; // Main classpublic class GFG {     // Main driver method    public static void main(String args[])    {        // Creating a calendar object        Calendar calndr = Calendar.getInstance();         // Displaying the current date        // using getTime() method        System.out.println("Current Date: "                           + calndr.getTime());         // Adding 30 days to the current calendar        // using add() method        calndr.add(Calendar.DATE, 30);         // Printing the corresponding date        System.out.println("After 30 days: "                           + calndr.getTime());         // Subtracting 3 months from the current calendar        calndr.add(Calendar.MONTH, -3);         // Printing the corresponding date        System.out.println("3 months ago it was: "                           + calndr.getTime());         // Subtracting 10 years from        // the Current calendar        calndr.add(Calendar.YEAR, -10);         // Printing the corresponding date        System.out.println("10 years ago it was: "                           + calndr.getTime());    }}
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
 
solankimayank
Java - util package
Java-Calendar
Java-Functions
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Different ways of Reading a text file in Java
Stream In Java
Constructors in Java
Generics in Java
Exceptions in Java
Functional Interfaces in Java
Comparator Interface in Java with Examples
Commonly Asked Java Programming Interview Questions | Set 2
HashMap get() Method in Java
Strings in Java