How to add 6 Months to the Current Date in Java?
In this tutorial, we will learn how to add 6 months to the local or current date in Java language. We will begin our topic with basic concepts and would reach a java code to finally understand the proper way to accomplish this task.
To add months to the current date, there exists a method known as the plus months()method, which belongs to the java class called LocalDate class.
This method is encompassed in Java version 8throughDataTime API.
Users can easily increase the months as per their requirements.
To be able to use this class, the needs need to import a package known as java.time package.
Let us see some of the examples to understand the working of the method.
Examples:
- Input - 2021-05-05 output - 2020-11-05. [ After adding 6 months]
- Input - 2022-06-05 output - 2022-12-05.[ After adding 6 months]
- Input - 2023-05-05 output - 2023-11-05.[ After adding 6 months]
- Input - 2020-05-05 output - 2020-11-05. [ After adding 6 months]
- Input - 2015-03-09 output - 2015-08-09. [ After adding 6 months]
Let us first see the syntax of the method to be discussed here.
Syntax:
public LocalDateplusmonths (long monthsTobeAdded)
Description of syntax:
- monthsTobeAdded– It is the only parameter of the method. It accepts the number of months which is to be added to the current date.
- – It is the name of the method that would increase the number of months.
- Return - A copy of the LocalDate is returned by the method plusMonths() after the addition of the required months.
- Exception - An exception known as DateTimeException is thrown in case the outcomeoverdoes the supported range of data.
Now, let us see the working of the above-stated method by using it in a java program.
Implementation 1:
import java.time.LocalDate;
//Example to Add 6 months to a current Date
public class Example1 {
public static void main(String[] args){
// considering a local date
LocalDate date1 = LocalDate.now();
// Displaying date
System.out.println("Current Date : "+date1);
// Adding 6 months to the date
LocalDate newDate1 = date1.plusMonths(6);
System.out.println("Date after 6 months : "+newDate1);
}
}
Output:

Explanation: The current date is 28th November 2022 and the date after 6 months would be 28th May 2023. Here, LocalDate.now()is being used to get the current date.
With the help of the above method, it reads the current date and gives the output accordingly.
Implementation 2:
import java.time.LocalDate;
//Example to add 6 months to a Date
public class Example2 {
public static void main(String[] args){
// Considering any date
LocalDate date1 = LocalDate.parse("2022-07-08");
// Displaying the given date
System.out.println("Given Date : "+date1);
// Displaying the date after the addition of 6 months
LocalDate newDate1 = date1.plusMonths(6);
System.out.println("New Date : "+newDate1);
}
}
Output:

Explanation: In this code, we have considered the date as 8th July 2022, and after increasing it by six months the new date becomes 18 January 2023. Here, the date is being inputted by the user manually unlike implementation 1. LocalDate.parse() is being used to get the date from the user.
Implementation 3:
import java.time.LocalDate;
//Example to add 6 months to a Date
public class Example3 {
public static void main(String[] args){
// Considering any date
LocalDate date1 = LocalDate.parse("2027-08-18");
// Displaying the given date
System.out.println("Given Date : "+date1);
// Displaying the date after the addition of 6 months
LocalDate newDate1 = date1.plusMonths(6);
System.out.println("New Date : "+newDate1);
}
}
Output:

Explanation: In this code, we have considered the date as 2027-08-18, and after increasing it by six months the new date becomes 2028-02-18. Here LocalDate.parse() is being used to get the date from the user.
Summary:
We learned how to add six months to the current date in Java language. It was interesting to understand we can accomplish this task simply by taking a method called plus months () into account and providing 6 as the parameter.
However, we can increase any number of months by giving that number as a parameter in the method. We can also decrease the no. of months by giving a negative integer as a parameter to the plus months() method.