Skip to content
Related Articles

Related Articles

Improve Article

Calendar add() Method in Java with Examples

  • Last Updated : 12 Feb, 2019

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:






// 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());
    }
}
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 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());
    }
}
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-

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :