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.
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:
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
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
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.