eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
announcement - icon

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

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New “REST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

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 – LJB – NPI EA (cat = Core Java)
announcement - icon

Code your way through and build up a solid, practical foundation of Java:

>> Learn Java Basics

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.
Baeldung Pro – NPI EA (cat = Baeldung)
announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

Once the early-adopter seats are all used, the price will go up and stay at $33/year.

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Course – LS – NPI EA (cat=REST)

announcement - icon

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

>> CHECK OUT THE COURSE

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.

eBook Jackson – NPI EA – 3 (cat = Jackson)