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

最后修改: 2022年 3月 26日

1. Overview

1.概述

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

在本教程中,我们将发现在Java中从Date对象中减去日子的各种方法。

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

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对象中减去天数

@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

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:

让我们使用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.

我们可以在关于迁移到新的Java 8日期时间API的文章中阅读更多关于与遗留代码的交互以及两个API之间的差异。

4. Joda-Time

4.约达-时间

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.

我们可以将Joda-Time作为Java最初的日期和时间处理解决方案的更好的替代方案。该库提供了一个更直观的API,多个日历系统,线程安全,以及不可变的对象。

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

为了使用Joda-Time,我们需要在pom.xml文件中将其作为一个依赖项。

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

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

让我们从Joda-Time的DateTime对象中减去5天。

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

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

5. Conclusion

5.总结

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.

像往常一样,源代码可在GitHub上获得。