Course – LS – All
announcement - icon

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

>> CHECK OUT THE COURSE

1. Overview

In this short tutorial, we’ll cast light on how to get the last day of a given month in Java.

First, we’ll highlight how to do it using core Java methods. Then, we will illustrate how to achieve the same objective using the Joda Time library.

2. Before Java 8

Before Java 8, Date and Calendar classes were good options to use to manipulate time and date in Java.

Typically, Calendar provides a set of methods and constants that we can use to access and manipulate temporal information such as days, months, and years.

So, let’s see how to use it to get the last day of a particular month:

static int getLastDayOfMonthUsingCalendar(int month) {
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.MONTH, month);
    return cal.getActualMaximum(Calendar.DAY_OF_MONTH);
}

As we can see, we create a Calendar object using Calendar.getInstance(). Then, we set the calendar month to the specified one. Furthermore, we use the getActualMaximum() method with the Calendar.DAY_OF_MONTH field to retrieve the last day of the month.

Now, let’s add a test case to confirm our method:

@Test
void givenMonth_whenUsingCalendar_thenReturnLastDay() {
    assertEquals(31, LastDayOfMonth.getLastDayOfMonthUsingCalendar(0));
    assertEquals(30, LastDayOfMonth.getLastDayOfMonthUsingCalendar(3));
    assertEquals(31, LastDayOfMonth.getLastDayOfMonthUsingCalendar(9));
}

An important point to mention here is that the month value is zero-based. For instance, the value of January is zero, so we need to subtract 1 from the desired month value.

3. Java 8 Date Time API

Java 8 comes with a lot of new features and enhancements. Among these features, we find the date time API. This API was introduced to overcome the limitations of the older API based on Date and Calendar classes.

So, let’s go down the rabbit hole and see what options this new API provides to get the last day of a month.

3.1. Using YearMonth

One of the most common ways to manipulate months is using the YearMonth class. As the name indicates, it denotes a combination of a year and a month.

This class provides the atEndOfMonth() method, which returns a LocalDate object that denotes the date at the end of the given month.

So, let’s see in practice:

static int getLastDayOfMonthUsingYearMonth(YearMonth date) {
    return date.atEndOfMonth()
      .getDayOfMonth();
}

In a nutshell, we used atEndOfMonth() to get the last date of the month. Then, we chained with getDayOfMonth() to retrieve the day of the returned LocalDate object.

As always, let’s add a test case:

@Test
void givenMonth_whenUsingYearMonth_thenReturnLastDay() {
    assertEquals(31, LastDayOfMonth.getLastDayOfMonthUsingYearMonth(YearMonth.of(2023, 1)));
    assertEquals(30, LastDayOfMonth.getLastDayOfMonthUsingYearMonth(YearMonth.of(2023, 4)));
    assertEquals(31, LastDayOfMonth.getLastDayOfMonthUsingYearMonth(YearMonth.of(2023, 10)));
}

3.2. Using TemporalAdjusters

Another solution would be using the TemporalAdjusters class. Typically, this class offers several ready-to-use static methods that we can use to adjust temporal objects.

So, let’s see together how to use it to get the last day of a LocalDate instance:

static int getLastDayOfMonthUsingTemporalAdjusters(LocalDate date) {
    return date.with(TemporalAdjusters.lastDayOfMonth())
      .getDayOfMonth();
}

As shown above, the class provides the adjuster lastDayOfMonth(). It returns a new date set to the last day of the month.

Furthermore, we used the getDayOfMonth() method to get the day of the returned date.

Next, we’ll confirm our method using a test case:

@Test
void givenMonth_whenUsingTemporalAdjusters_thenReturnLastDay() {
    assertEquals(31, LastDayOfMonth.getLastDayOfMonthUsingTemporalAdjusters(LocalDate.of(2023, 1, 1)));
    assertEquals(30, LastDayOfMonth.getLastDayOfMonthUsingTemporalAdjusters(LocalDate.of(2023, 4, 1)));
    assertEquals(31, LastDayOfMonth.getLastDayOfMonthUsingTemporalAdjusters(LocalDate.of(2023, 10, 1)));
}

4. Joda Time Library

Alternatively, we can use the Joda Time API to accomplish the same purpose. It was a de facto standard for date-time manipulation before Java 8. First, we need to add the Joda Time dependency to the pom.xml file:

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

Similarly, Joda Time offers the LocalDate class to represent a date. This includes the withMaximumValue() method that can be used to obtain the last day of the month.

Let’s see in action:

static int getLastDayOfMonthUsingJodaTime(org.joda.time.LocalDate date) {
    return date.dayOfMonth()
      .withMaximumValue()
      .getDayOfMonth();
}

Lastly, let’s create another test case:

@Test
void givenMonth_whenUsingJodaTime_thenReturnLastDay() {
    assertEquals(31, LastDayOfMonth.getLastDayOfMonthUsingJodaTime(org.joda.time.LocalDate.parse("2023-1-1")));
    assertEquals(30, LastDayOfMonth.getLastDayOfMonthUsingJodaTime(org.joda.time.LocalDate.parse("2023-4-1")));
    assertEquals(31, LastDayOfMonth.getLastDayOfMonthUsingJodaTime(org.joda.time.LocalDate.parse("2023-10-1")));
}

Unsurprisingly, the test passes successfully.

5. Conclusion

In this article, we explored different ways of getting the last day of a given month in Java. Along the way, we explained how to do it using core Java. Then, we demonstrated how to use third-party libraries such as Joda Time.

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

Course – LS – All
announcement - icon

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

>> CHECK OUT THE COURSE

res – REST with Spring (eBook) (everywhere)