Course – LS – All

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

>> CHECK OUT THE COURSE

1. Overview

In this tutorial, we’ll discover various ways to subtract days from a Date object in Java.

We’ll start by using the Date Time API introduced with Java 8. After this, we’ll learn how to do it by using the classes from the java.util package, and, finally, we’ll achieve the same thing with the help of Joda-Time library.

2. java.time.LocalDateTime

The Date/Time API introduced in Java 8 is currently the most viable option for date and time calculations.

Let’s see how to subtract days from Java 8’s java.util.LocalDateTime object:

@Test
public void givenLocalDate_whenSubtractingFiveDays_dateIsChangedCorrectly() {
    LocalDateTime localDateTime = LocalDateTime.of(2022, 4, 20, 0, 0);

    localDateTime = localDateTime.minusDays(5);

    assertEquals(15, localDateTime.getDayOfMonth());
    assertEquals(4, localDateTime.getMonthValue());
    assertEquals(2022, localDateTime.getYear());
}

3. java.util.Calendar

Date and Calendar from java.util are the most commonly used pre-Java 8 utility classes for date operations.

Let’s subtract five days from a date using java.util.Calendar:

@Test
public void givenCalendarDate_whenSubtractingFiveDays_dateIsChangedCorrectly() {
    Calendar calendar = Calendar.getInstance();
    calendar.set(2022, Calendar.APRIL, 20);

    calendar.add(Calendar.DATE, -5);

    assertEquals(15, calendar.get(Calendar.DAY_OF_MONTH));
    assertEquals(Calendar.APRIL, calendar.get(Calendar.MONTH));
    assertEquals(2022, calendar.get(Calendar.YEAR));
}

We should be careful when using them as they have a few design flaws and aren’t thread-safe.

We can read more about the interaction with legacy code and the differences between the two APIs in the article about Migrating to the New Java 8 Date Time API.

4. Joda-Time

We can use Joda-Time as a better alternative to Java’s initial solution for date and time processing. The library provides a more intuitive API, multiple calendar systems, thread safety, and immutable objects.

In order to use Joda-Time, we need to include it as a dependency in the pom.xml file:

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

Let’s subtract five days from Joda-Time’s DateTime object:

@Test
public void givenJodaDateTime_whenSubtractingFiveDays_dateIsChangedCorrectly() {
    DateTime dateTime = new DateTime(2022, 4, 20, 12, 0, 0);

    dateTime = dateTime.minusDays(5);

    assertEquals(15, dateTime.getDayOfMonth());
    assertEquals(4, dateTime.getMonthOfYear());
    assertEquals(2022, dateTime.getYear());
}

Joda-Time is a good solution for legacy code. However, the project is officially “finished”, and migration to Java 8’s Date/Time API is recommended by the author of the library.

5. Conclusion

In this short article, we have explored several ways to subtract days from a date object.

We have achieved this using java.time.LocalDateTime and the pre-Java 8 solutions: java.util.Calendar and the Joda-Time library.

As usual, the source code is available 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)
2 Comments
Oldest
Newest
Inline Feedbacks
View all comments
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.