Core Java

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

OptionProsCons
Java LocalDate (Java 8 Date/Time API)
  • Built-in date and time API in Java 8 and later versions.
  • Provide a straightforward way to handle dates without external dependencies.
  • Supports date arithmetic and manipulation methods.
  • Allow for easy conversion to different formats.
  • Limited formatting options compared to Joda-Time and Apache Commons Lang3.
  • Requires a Java version of 8 or later.
Joda-Time
  • Well-established and widely used date and time library.
  • Offers a comprehensive API for date and time manipulation.
  • Provides advanced features like precise control over date calculations.
  • Supports date formatting and parsing.
  • Requires adding the Joda-Time library as a dependency.
  • Considered deprecated in favor of the Java 8 Date/Time API.
Apache Commons Lang3
  • Contains utility classes for data manipulation and formatting.
  • Provides convenience methods for adding one month to date.
  • Offers additional utilities for general-purpose programming tasks.
  • Lacks a dedicated date and time API, so it may not offer the same level of functionality as Joda-Time or the Java 8 Date/Time API.
  • Requires adding the Apache Commons Lang3 library as a dependency.

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 LocalDate class from the java.time package, which is part of the Java 8 Date/Time API.
  • Inside the main method, we obtain the current date using the now() method of the LocalDate class and store it in the currentDate variable.
  • We then print the current date using System.out.println.
  • Next, we add 1 month to the currentDate by calling the plusMonths(1) method, which returns a new LocalDate object with the desired addition. We assign this new date to the futureDate variable.
  • 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 LocalDate class from the org.joda.time package, which is part of the Joda-Time library.
  • Inside the main method, we create a new LocalDate object 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 currentDate by calling the plusMonths(1) method, which returns a new LocalDate object with the desired addition. We assign this new date to the futureDate variable.
  • 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 DateUtils class from the org.apache.commons.lang3.time package, which is part of the Apache Commons Lang3 library.
  • Inside the main method, we create a new Date object 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 currentDate by calling the addMonths method from the DateUtils class, passing in the currentDate and the number of months to add (in this case, 1). The method returns a new Date object representing the date after adding 1 month. We assign this new date to the futureDate variable.
  • 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.

Download
You can download the files of this example here: Adding One Month to Current Date in Java

Yatin

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button