Subtract Days from a Date in Java – 在Java中从一个日期中减去天数

最后修改: 2022年 3月 26日

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.

我们将首先使用Java 8引入的日期时间API。之后,我们将学习如何通过使用java.util包中的类来实现,最后,我们将在Joda-Time库的帮助下实现同样的事情。

2. java.time.LocalDateTime

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

Java 8中引入的Date/Time API是目前最可行的日期和时间计算的选择。

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

让我们看看如何从Java 8的java.util.LocalDateTime对象中减去天数

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.

来自java.utilDateCalendar是最常用的前Java 8实用类,用于日期操作。

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


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.

我们可以在关于迁移到新的Java 8日期时间API的文章中阅读更多关于与遗留代码的交互以及两个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:



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


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.

Joda-Time是遗留代码的一个很好的解决方案。然而,该项目已正式 “结束”,库的作者建议迁移到Java 8的日期/时间API。

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.

我们使用java.time.LocalDateTime和Java 8之前的解决方案实现了这一点。java.util.Calendar和Joda-Time库。

As usual, the source code is available over on GitHub.