Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

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.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW