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.
DeclarationFollowing 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.
This method does not return any value.
ExceptionNA
Adding Days to Current Dated Calendar Instance ExampleThe 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 2022Subtracting 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 2022Subtracting 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
java_util_calendar.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4