
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Add months to current date using Calendar.add() method in Java
The given task is to add months to a date (object) using the add() method of the Calendar class. For example, if we need to add 5 months to the date: 26-06-2025, the new date would be 26-11-2025.
Calendar.add() method in Java
The calender.add() method in Java is used to add a specified amount of time to any field of the Calendar. Using this method, we can add months, years, days, etc. This method accepts two parameters: the first one is the field we want to add to, and the second one is the amount we want to add. To add months, the field should be MONTH.
Syntax of the add() method
Following is the syntax of the calender.add() method:
add(Calendar.MONTH, amount);
Example: Adding Months to a Date
In the following example, we will create a Calendar object and then add 5 months to the current date using the calendar.add() method.
import java.util.Calendar; public class Demo { public static void main(String[] args) { Calendar calendar = Calendar.getInstance(); System.out.println("Current Date = " + calendar.getTime()); // Add 5 months to current date calendar.add(Calendar.MONTH, 5); System.out.println("Updated Date = " + calendar.getTime()); } }
Following is the output of the above program -
Current Date = Thu Nov 22 16:30:08 UTC 2018 Updated Date = Mon Apr 22 16:30:08 UTC 2019