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 4th 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 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Overview

In this short article, we’ll highlight how to get the first date of the current month in Java.

First, we’re going to elucidate how to do it using JDK solutions. Then, we will showcase how to achieve the same outcome using third-party libraries such as Joda Time.

2. Introduction to the Problem

As always, let’s understand the main problem through an example. Let’s assume that our current date is 2023-10-15:

LocalDate currentDate = LocalDate.of(2023, 10, 15);

Here, we aim to get the first date of October which is 2023-10-01. So, let’s dive into the code.

3. Using the Calendar Class

The Calendar class provides a set of handy methods that we can use to manipulate dates and times. It offers a convenient way to get the first date of a given month.

So, let’s see it in action:

@Test
void givenMonth_whenUsingCalendar_thenReturnFirstDay() {
    Date currentDate = new GregorianCalendar(2023, Calendar.NOVEMBER, 23).getTime();
    Date expectedDate = new GregorianCalendar(2023, Calendar.NOVEMBER, 1).getTime();

    Calendar cal = Calendar.getInstance();
    cal.setTime(currentDate);
    cal.set(Calendar.DAY_OF_MONTH, 1);

    assertEquals(expectedDate, cal.getTime());
}

As we can see, we alter the calendar time to the given date. Then, we call set(Calendar.DAY_OF_MONTH, 1) to set the day of the current month to 1.

Furthermore, we return the new date, which denotes the first date of the month using the getTime() method.

4. Using the Java 8 Date Time API

Java 8 comes with a host of ready-to-use features such as the date time API. This new API was introduced to address the drawbacks of the older API based on the Calendar class.

Please note that this new Java 8 API is the best way to go when it comes to working with dates.

So, let’s go down the rabbit hole and see what methods this new API offers to answer our central question.

4.1. Using the LocalDate Class

LocalDate is a great option to consider if we want to get the first day of the current month. This class provides the withDayOfMonth() method that we can use to return a new date with the day altered.

So, let’s see in practice:

@Test
void givenMonth_whenUsingLocalDate_thenReturnFirstDay() {
    LocalDate currentDate = LocalDate.of(2023, 9, 6);
    LocalDate expectedDate = LocalDate.of(2023, 9, 1);

    assertEquals(expectedDate, currentDate.withDayOfMonth(1));
}

As shown above, we used withDayOfMonth(1) to set the day of the specified date to the value 1. That way, we get the first date of the month.

4.2. Using the TemporalAdjusters Class

Similarly, we can use TemporalAdjusters to tackle our challenge. As the name implies, this class represents several adjusters that aim to modify temporal objects such as days, and months.

Among these adjusters, we find firstDayOfMonth. Next, let’s add a new test case to test our adjuster:

@Test
void givenMonth_whenUsingTemporalAdjusters_thenReturnFirstDay() {
    LocalDate currentDate = LocalDate.of(2023, 7, 19);
    LocalDate expectedDate = LocalDate.of(2023, 7, 1);

    assertEquals(expectedDate, currentDate.with(TemporalAdjusters.firstDayOfMonth()));
}

Typically, firstDayOfMonth returns the first day of a particular month. Please note that we used the with() method to get the new adjusted copy of the date.

4.3. Using the YearMonth Class

YearMonth introduced the atDay() method to combine a specific year/month with the day passed as a parameter.

Now, let’s see how to use this method to achieve our objective:

@Test
void givenMonth_whenUsingYearMonth_thenReturnFirstDay() {
    YearMonth currentDate = YearMonth.of(2023, 4);
    LocalDate expectedDate = LocalDate.of(2023, 4, 1);

    assertEquals(expectedDate, currentDate.atDay(1));
}

Here, we specified 1 as the parameter to denote the first day of the given year and month.

5. Using the Joda Time Library

Another solution would be using the Joda Time library. It provides a standard API for date-time manipulation before Java 8.

Before starting working with this library, we need first to add its dependency to the pom.xml file:

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

Joda Time comes with a variant of the LocalDate class to represent a date. So, let’s exemplify the use of this class with a new test case:

@Test
void givenMonth_whenUsingJodaTime_thenReturnFirstDay() {
    org.joda.time.LocalDate currentDate = org.joda.time.LocalDate.parse("2023-5-10");
    org.joda.time.LocalDate expectedDate = org.joda.time.LocalDate.parse("2023-5-1");

    assertEquals(expectedDate, currentDate.dayOfMonth()
      .withMinimumValue());
}

As the name indicates, dayOfMonth() allows retrieving the day of the given month. Furthermore, we used the withMinimumValue() method to set the day of the returned date to the minimum value which is 1.

6. Conclusion

In this short tutorial, we explored different ways of getting the first day of the current month in Java.

First, we discussed how to do it using JDK classes. Then, we illustrated how to accomplish the same objective using Joda Time API.

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 4th 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 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW