- Java.util Package Classes
- Java.util - Home
- Java.util - ArrayDeque
- Java.util - ArrayList
- Java.util - Arrays
- Java.util - BitSet
- Java.util - Calendar
- Java.util - Collections
- Java.util - Currency
- Java.util - Date
- Java.util - Dictionary
- Java.util - EnumMap
- Java.util - EnumSet
- Java.util - Formatter
- Java.util - GregorianCalendar
- Java.util - HashMap
- Java.util - HashSet
- Java.util - Hashtable
- Java.util - IdentityHashMap
- Java.util - LinkedHashMap
- Java.util - LinkedHashSet
- Java.util - LinkedList
- Java.util - ListResourceBundle
- Java.util - Locale
- Java.util - Observable
- Java.util - PriorityQueue
- Java.util - Properties
- Java.util - PropertyPermission
- Java.util - PropertyResourceBundle
- Java.util - Random
- Java.util - ResourceBundle
- Java.util - ResourceBundle.Control
- Java.util - Scanner
- Java.util - ServiceLoader
- Java.util - SimpleTimeZone
- Java.util - Stack
- Java.util - StringTokenizer
- Java.util - Timer
- Java.util - TimerTask
- Java.util - TimeZone
- Java.util - TreeMap
- Java.util - TreeSet
- Java.util - UUID
- Java.util - Vector
- Java.util - WeakHashMap
- Java.util Package Extras
- Java.util - Interfaces
- Java.util - Exceptions
- Java.util - Enumerations
- Java.util Useful Resources
- Java.util - Useful Resources
- Java.util - Discussion
Java Calendar add() Method
Description
The Java Calendar add() adds or subtracts the specified amount of time (amount) to the given calendar field (field), based on the calendar's rules.
Declaration
Following is the declaration for java.util.Calendar.add() method
public abstract void add(int field,int amount)
Parameters
field − the calendar field.
amount −the amount of date or time to be added to the field.
Return Value
This method does not return any value.
Exception
NA
Adding Days to Current Dated Calendar Instance Example
The following example shows the usage of Java Calendar add() method. We're creating a Calendar instance of current date and adding 20 days to it using add method and then printing the updated date.
package com.tutorialspoint; import java.util.Calendar; public class CalendarDemo { public static void main(String[] args) { // create a calendar Calendar cal = Calendar.getInstance(); // print current date System.out.println("The current date is : " + cal.getTime()); // add 20 days to the calendar cal.add(Calendar.DATE, 20); System.out.println("20 days later: " + cal.getTime()); } }
Output
Let us compile and run the above program, this will produce the following result −
The current date is : Fri Sep 23 14:27:57 IST 2022 20 days later: Thu Oct 13 14:27:57 IST 2022
Subtracting Months to Current Dated Calendar Instance Example
The following example shows the usage of Java Calendar add() method. We're creating a Calendar instance of current date and subtracting 2 months from it using add method and then printing the updated date.
package com.tutorialspoint; import java.util.Calendar; public class CalendarDemo { public static void main(String[] args) { // create a calendar Calendar cal = Calendar.getInstance(); // print current date System.out.println("The current date is : " + cal.getTime()); // subtract 2 months from the calendar cal.add(Calendar.MONTH, -2); System.out.println("2 months ago: " + cal.getTime()); } }
Output
Let us compile and run the above program, this will produce the following result −
The current date is : Fri Sep 23 14:28:22 IST 2022 2 months ago: Sat Jul 23 14:28:22 IST 2022
Subtracting Years from Current Dated Calendar Instance Example
The following example shows the usage of Java Calendar add() method. We're creating a Calendar instance of current date and subtracting 5 years from it using add method and then printing the updated date.
package com.tutorialspoint; import java.util.Calendar; public class CalendarDemo { public static void main(String[] args) { // create a calendar Calendar cal = Calendar.getInstance(); // print current date System.out.println("The current date is : " + cal.getTime()); // subtract 5 year from the calendar cal.add(Calendar.YEAR, -5); System.out.println("5 years ago: " + cal.getTime()); } }
Output
Let us compile and run the above program, this will produce the following result −
The current date is : Fri Sep 23 14:28:53 IST 2022 5 years ago: Sat Sep 23 14:28:53 IST 2017