Adding One Month to Current Date in Java
Hello. In this tutorial, we will explore the different Date packages in Java and do a simple example of adding one more month to the current date.
1. Introduction
In Java, you can use the java.time.LocalDate class from the Java 8 Date/Time API or later versions to add one month to the current date. The plusMonths() method in Java is a part of the java.time.LocalDate class and is used to add a specified number of months to a given date. Here’s an explanation of the method:
Method syntax
public LocalDate plusMonths(long monthsToAdd)
Parameters: monthsToAdd: The number of months to add to the current date. It can be a positive or negative value.
Return Value: The method returns a new LocalDate object that represents the resulting date after adding the specified number of months.
1.1 Comparison between Java Local Date, Joda Time, and Apache Common Lang 3
| Option | Pros | Cons |
|---|---|---|
| Java LocalDate (Java 8 Date/Time API) |
|
|
| Joda-Time |
|
|
| Apache Commons Lang3 |
|
|
2. Add one month to current date examples
2.1 LocalDate example to Add
Here’s an example in Java 8 using the LocalDate class to add 1 month to the current date.
Example
import java.time.LocalDate;
public class AddOneMonthExample {
public static void main(String[] args) {
// Get the current date
LocalDate currentDate = LocalDate.now();
System.out.println("Current Date: " + currentDate);
// Add 1 month to the current date
LocalDate futureDate = currentDate.plusMonths(1);
System.out.println("Date after adding 1 month: " + futureDate);
}
}
// Output
Current Date: 2023-06-27
Date after adding 1 month: 2023-07-27
2.1.1 Explanation of the code
- The code begins by importing the
LocalDateclass from thejava.timepackage, which is part of the Java 8 Date/Time API. - Inside the
mainmethod, we obtain the current date using thenow()method of theLocalDateclass and store it in thecurrentDatevariable. - We then print the current date using
System.out.println. - Next, we add 1 month to the
currentDateby calling theplusMonths(1)method, which returns a newLocalDateobject with the desired addition. We assign this new date to thefutureDatevariable. - Finally, we print the date after adding 1 month using
System.out.println.
When you run this code, it will output the current date and the date after adding 1 month, based on your system’s current date.
Note: The Java 8 Date/Time API provides immutability, so the original currentDate remains unchanged, and the plusMonths method returns a new LocalDate object representing the modified date.
2.2 Joda-Time example
Here’s an example of adding one more month to the current date using Joda-Time.
Example
import org.joda.time.LocalDate;
public class AddOneMonthExample2 {
public static void main(String[] args) {
// Get the current date
LocalDate currentDate = new LocalDate();
System.out.println("Current Date: " + currentDate);
// Add 1 month to the current date
LocalDate futureDate = currentDate.plusMonths(1);
System.out.println("Date after adding 1 month: " + futureDate);
}
}
// Output
Current Date: 2023-06-27
Date after adding 1 month: 2023-07-27
2.2.1 Explanation of the code
- The code begins by importing the
LocalDateclass from theorg.joda.timepackage, which is part of the Joda-Time library. - Inside the
mainmethod, we create a newLocalDateobject using the no-args constructor, which represents the current date. - We then print the current date using
System.out.println. - Next, we add 1 month to the
currentDateby calling theplusMonths(1)method, which returns a newLocalDateobject with the desired addition. We assign this new date to thefutureDatevariable. - Finally, we print the date after adding 1 month using
System.out.println.
When you run this code, it will output the current date and the date after adding 1 month based on your system’s current date using Joda-Time.
2.3 Apache Commons Lang3 example
Here’s an example of adding one more month to the current date using Apache Commons Lang3.
Example
import org.apache.commons.lang3.time.DateUtils;
import java.util.Date;
public class AddOneMonthExample3 {
public static void main(String[] args) {
// Get the current date
Date currentDate = new Date();
System.out.println("Current Date: " + currentDate);
// Add 1 month to the current date
Date futureDate = DateUtils.addMonths(currentDate, 1);
System.out.println("Date after adding 1 month: " + futureDate);
}
}
// Output
Current Date: 2023-06-27
Date after adding 1 month: 2023-07-27
2.3.1 Explanation of the code
- The code begins by importing the
DateUtilsclass from theorg.apache.commons.lang3.timepackage, which is part of the Apache Commons Lang3 library. - Inside the
mainmethod, we create a newDateobject using the no-args constructor, which represents the current date. - We then print the current date using
System.out.println. - Next, we add 1 month to the
currentDateby calling theaddMonthsmethod from theDateUtilsclass, passing in thecurrentDateand the number of months to add (in this case, 1). The method returns a newDateobject representing the date after adding 1 month. We assign this new date to thefutureDatevariable. - Finally, we print the date after adding 1 month using
System.out.println.
When you run this code, it will output the current date and the date after adding 1 month based on your system’s current date using Apache Commons Lang3.
This concludes our tutorial, and I trust that the article provided you with the information you sought. I wish you happy learning and encourage you to share your newfound knowledge with others!
3. Conclusion
In conclusion, adding 1 month to the current date can be easily accomplished using various date and time libraries available in Java.
In Java 8 and later versions, the LocalDate class from the Java Date/Time API provides a built-in solution. By using the plusMonths(1) method on a LocalDate object representing the current date, we can obtain a new LocalDate object representing the date after adding 1 month. This approach is convenient, requires no external dependencies, and allows for easy manipulation of dates.
Alternatively, libraries like Joda-Time and Apache Commons Lang3 also offer solutions for adding 1 month to date. Joda-Time provides a comprehensive date and time API, while Apache Commons Lang3 offers utility classes for date manipulation.
It’s important to choose the approach that best suits your project’s requirements and dependencies. The Java 8 Date/Time API is recommended for projects using Java 8 or later versions, while Joda-Time and Apache Commons Lang3 are viable options for older Java versions or when additional functionalities are needed.
Overall, adding 1 month to the current date is a common operation in date manipulation, and with the available libraries and APIs in Java, it can be easily implemented with the desired level of functionality and compatibility.
4. Download the Files
This was a tutorial on the plusMonth() method in Java programming.
You can download the files of this example here: Adding One Month to Current Date in Java

