Home »
Java »
Java Programs
Java program to add months to the current date
Java example to demonstrate how to add months to the current date?
Submitted by Nidhi, on April 02, 2022
Problem statement
In this program, we will create an instance of the Calendar class and add months into the current date using add() method, and printed the result.
Java program to add months to the current date
The source code to add months to the current date is given below. The given program is compiled and executed successfully.
// Java program to add months
// to the current date
import java.util.*;
public class Main {
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MONTH, 3);
System.out.println("Date after 3 months: " + cal.getTime());
}
}
Output
Date after 3 months: Sat Jul 02 05:53:39 GMT 2022
Explanation
In the above program, we created a class Main that contains a static method main(). The main() method is the entry point for the program, here we created the object of the Calendar class and added the 3 months to the current date using add() method, and printed the result.
Java Date and Time Programs »