Course – LS – All

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

>> CHECK OUT THE COURSE

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.

As always, the code 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.