Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

In this short tutorial, we’ll learn how to add one month to the current date in Java.

First, we’ll understand how to do this using core Java methods. Then, we’ll see how to accomplish the same using external libraries such as Joda-Time and Apache Commons Lang3.

2. Core Java Methods

Java provides several convenient ways to manipulate the date and time. Let’s explore different options to add one month to the current date.

2.1. Using Calendar Class

For versions earlier than Java 8, we can use Calendar to handle temporal data. This class offers a set of methods that we can use to manipulate date and time.

So, let’s  see it in action:

@Test
void givenCalendarDate_whenAddingOneMonth_thenDateIsChangedCorrectly() {
    Calendar calendar = Calendar.getInstance();
    // Dummy current date
    calendar.set(2023, Calendar.APRIL, 20);

    // add one month
    calendar.add(Calendar.MONTH, 1);

    assertEquals(Calendar.MAY, calendar.get(Calendar.MONTH));
    assertEquals(20, calendar.get(Calendar.DAY_OF_MONTH));
    assertEquals(2023, calendar.get(Calendar.YEAR));
}

As we can see, we used the add() method to add exactly one month to the given date. Calendar.MONTH is the constant that denotes months.

2.2. Using Date Class

Date class is another option to consider if we want to change the month of a particular date. However, the drawback of this choice is that this class is deprecated.

Let’s see the use of Date using a test case:

@SuppressWarnings("deprecation")
@Test
void givenDate_whenAddingOneMonth_thenDateIsChangedCorrectly() {
    Date currentDate = new Date(2023, Calendar.DECEMBER, 20);
    Date expectedDate = new Date(2024, Calendar.JANUARY, 20);

    currentDate.setMonth(currentDate.getMonth() + 1);

    assertEquals(expectedDate, currentDate);
}

As we can see above, the Date class provides the getMonth() method which returns a number representing the month. Furthermore, we added 1 to the returned number. Then, we called setMonth() to update the Date object with the new month.

Notably, it’s always recommended to use the new Date/Time API of Java 8 instead of the old one.

2.3. Using LocalDate Class

Similarly, we can use the LocalDate class introduced in Java 8. This class offers a straightforward and concise way to add months to a specific date through the plusMonths() method:

@Test
void givenJavaLocalDate_whenAddingOneMonth_thenDateIsChangedCorrectly() {
    LocalDate localDate = LocalDate.of(2023, 12, 20);

    localDate = localDate.plusMonths(1);

    assertEquals(1, localDate.getMonthValue());
    assertEquals(20, localDate.getDayOfMonth());
    assertEquals(2024, localDate.getYear());
}

Unsurprisingly, the test case passes with success.

3. Using Joda-Time

If Java 8 isn’t an option, we can opt for the Joda-Time library to achieve the same objective.

First, we need to add its dependency to the pom.xml file:

<dependency>
    <groupId>joda-time</groupId>
    <artifactId>joda-time</artifactId>
    <version>2.10</version>
</dependency>

Joda-Time provides its own version of the LocalDate class. So, let’s see how we can use it to add one month:

@Test
void givenJodaTimeLocalDate_whenAddingOneMonth_thenDateIsChangedCorrectly() {
    org.joda.time.LocalDate localDate = new org.joda.time.LocalDate(2023, 12, 20);

    localDate = localDate.plusMonths(1);

    assertEquals(1, localDate.getMonthOfYear());
    assertEquals(20, localDate.getDayOfMonth());
    assertEquals(2024, localDate.getYear());
}

As we can see, LocalDate also comes with the same method plusMonths(). As the name indicates, it allows us to add a certain number of months, which is 1 in our case.

4. Using Apache Commons Lang3

Alternatively, we can use the Apache Commons Lang3 library. As usual, to get started using this library, we first need to add the Maven dependency :

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.14.0</version>
</dependency>

Typically, Apache Commons Lang3 provides the DateUtils utility class to perform a bunch of date operations.

Let’s see how to use it using a practical example:

@Test
void givenApacheCommonsLangDateUtils_whenAddingOneMonth_thenDateIsChangedCorrectly() {
    Date currentDate = new GregorianCalendar(2023, Calendar.APRIL, 20, 4, 0).getTime();
    Date expectedDate = new GregorianCalendar(2023, Calendar.MAY, 20, 4, 0).getTime();

    assertEquals(expectedDate, DateUtils.addMonths(currentDate, 1));
}

In a nutshell, we used the addMonths() method to increment the specified month by one. An important point to note here is that this method returns a new Date object. The original object remains unchanged.

5. Conclusion

In this short article, we explored different ways of adding one month to the current date in Java.

First, we saw how to do this using core Java classes. Then, we learned how to achieve the same using third-party libraries such as Joda Time and Apache Commons Lang3.

As always, the code samples used in this article can be found over on GitHub.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.