Open In App

Calendar add() Method in Java with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

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 classes
import java.util.Calendar;
 
// Main class
public 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());
    }
}


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:

Java




// Java Program to Illustrate add() Method
// of Calendar class
 
// Importing required classes
import java.util.Calendar;
 
// Main class
public 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());
    }
}


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

 



Last Updated : 02 Nov, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads