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 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.

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