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 explore different ways of getting yesterday’s date in Java.

First, we’ll explain how to do it using core Java. Then, we will demonstrate how to tackle our main puzzle using external libraries such as Joda-Time and Apache Commons Lang.

2. Before Java 8

Before Java 8, we would usually use Date or Calendar to handle and manipulate date/time information. So, let’s see how to use these two classes to get yesterday’s date.

2.1. Using Date

The Date class denotes a specific instant in time. It provides a set of methods to manipulate and retrieve information about dates. However, it’s important to mention that this class is outdated and marked as deprecated.

So, let’s see in action:

@Test
void givenDate_whenUsingDateClass_thenReturnYesterday() {
    Date date = new Date(2023, Calendar.DECEMBER, 20);
    Date yesterdayDate = new Date(date.getTime() - 24 * 60 * 60 * 1000);
    Date expectedYesterdayDate = new Date(2023, Calendar.DECEMBER, 19);

    assertEquals(expectedYesterdayDate, yesterdayDate);
}

As we can see, we used the getTime() method to get the number of milliseconds of the given date. Then, we subtracted a day which is 24 * 60 * 60 * 1000 in milliseconds.

2.2. Using Calendar

Calendar is another option to consider if we want to work with the old API. This class comes with a set of methods to manage temporal data, such as days and months.

For instance, we can use the add() method to add a certain number of days. Since we want to get yesterday’s date, we will need to specify -1 as the value.

So, let’s see it in practice:

@Test
void givenDate_whenUsingCalendarClass_thenReturnYesterday() {
    Calendar date = new GregorianCalendar(2023, Calendar.APRIL, 20, 4, 0);
    date.add(Calendar.DATE, -1);
    Calendar expectedYesterdayDate = new GregorianCalendar(2023, Calendar.APRIL, 19, 4, 0);

    assertEquals(expectedYesterdayDate, date);
}

As expected, the test passed with success.

3. Java 8 Date-Time API

Java 8 often gets praised for its new Date-Time feature. This API comes with a host of ready-to-use classes and methods for date and time calculations in a more concise and human-friendly manner. So, let’s take a close look at how to use the new Date-Time API to get yesterday’s date.

3.1. Using LocalDate

One of the easiest solutions is using the LocalDate class. It provides the minusDays() method to subtract a specific number of days from the given date. In our case, we will need to subtract exactly one day.

Now, let’s exemplify the use of LocalDate.minusDays() with a test case:

@Test
void givenDate_whenUsingLocalDateClass_thenReturnYesterday() {
    LocalDate localDate = LocalDate.of(2023, 12, 20);
    LocalDate yesterdayDate = localDate.minusDays(1);
    LocalDate expectedYesterdayDate = LocalDate.of(2023, 12, 19);

    assertEquals(expectedYesterdayDate, yesterdayDate);
}

As shown above, minusDays(1) returns a new LocalDate object that denotes yesterday.

3.2. Using Instant

Another solution would be using the Instant class. As the name implies, it models a specific time point in the timeline. Typically, the Instant class comes with the minus() method that we can use to subtract a specific amount of milliseconds.

So, let’s illustrate using a practical example how to use it to get yesterday’s date:

@Test
void givenDate_whenUsingInstantClass_thenReturnYesterday() {
    Instant date = Instant.parse("2023-10-25");
    Instant yesterdayDate = date.minus(24 * 60 * 60 * 1000);
    Instant expectedYesterdayDate = Instant.parse("2023-10-24");

    assertEquals(expectedYesterdayDate, yesterdayDate);
}

As we mentioned earlier, 24 * 60 * 60 * 1000 represents a day in milliseconds. So here, we subtract a day from the given date.

4. Using Joda-Time

Similarly, we can use the Joda-Time API to answer our central question. 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 is the de facto standard for date and time manipulation before the release of Java 8. It offers its own version of LocalDate class. So, let’s see in practice:

@Test
void givenDate_whenUsingJodaTimeLocalDateClass_thenReturnYesterday() {
    org.joda.time.LocalDate localDate = new org.joda.time.LocalDate(2023, 12, 20);
    org.joda.time.LocalDate yesterdayDate = localDate.minusDays(1);
    org.joda.time.LocalDate expectedYesterdayDate = new org.joda.time.LocalDate(2023, 12, 19);

    assertEquals(expectedYesterdayDate, yesterdayDate);
}

In a nutshell, the class provides the same exact method minusDays() that we can use to minus a specific number of days as its name indicates.

5. Using Apache Commons Lang3

On the flip side, we can opt for the Apache Commons Lang3 library. As always, we need to add the Maven dependency before starting to use it:

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

Now, let’s demonstrate how to use this library to get yesterday’s date:

@Test
void givenDate_whenUsingApacheCommonsLangDateUtils_thenReturnYesterday() {
    Date date = new GregorianCalendar(2023, Calendar.MAY, 16, 4, 0).getTime();
    Date yesterdayDate = DateUtils.addDays(date, -1);
    Date expectedYesterdayDate = new GregorianCalendar(2023, Calendar.MAY, 15, 4, 0).getTime();

    assertEquals(expectedYesterdayDate, yesterdayDate);
}

The Apache Commons Lang3 library provides the DateUtils() class for date-time operations. This utility class offers the addDays() method to add a number of days which is -1 in our case.

Please note that the method returns a new Date object. The original given date remains unchanged.

6. Conclusion

In this short article, we explained in detail how to get yesterday’s date in Java. Throughout the course, we illustrated how to do it using core Java classes. Then, we showcased how to use external libraries such as Apache Commons Lang3 and Joda-Time.

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.