Iterate Through a Range of Dates in Java – 在Java中遍历一个日期范围

最后修改: 2018年 8月 23日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this quick tutorial, we’ll study several ways to iterate over a range of dates, using a start and end date, in Java 7, Java 8, and Java 9.

在这个快速教程中,我们将学习在Java 7、Java 8和Java 9中使用开始和结束日期对一系列日期进行迭代的几种方法。

2. Java 7

2.Java 7

Starting with Java 7, we’ll use the class java.util.Date for holding the date values and java.util.Calendar for incrementing from one date to the next.

从Java 7开始,我们将使用java.util.Date类来保存日期值,java.util.Calendar用于从一个日期递增到下一个。

Let’s see an example using a simple while loop along with the java.util.Date and java.util.Calendar classes:

让我们看一个例子,使用一个简单的while循环,以及java.util.Datejava.util.Calendar类。

void iterateBetweenDatesJava7(Date start, Date end) {
    Date current = start;

    while (current.before(end)) {
        processDate(current);

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(current);
        calendar.add(Calendar.DATE, 1);
        current = calendar.getTime();
    }
}

3. Java 8

3.JAVA 8

Starting with Java 8, we can use the new Java 8 Date API. This gives us self-handling, immutable, fluent, and thread-safe objects. It also allows us to write cleaner code without the need for a helper class like java.util.Calendar for incrementing the dates.

从Java 8开始,我们可以使用新的Java 8 Date API。这为我们提供了自处理的、不可变的、流畅的和线程安全的对象。它还允许我们编写更简洁的代码,而不需要像java.util.Calendar这样的辅助类来递增日期。

Let’s use a simple for loop, the LocalDate class, and the method plusDays(1) to move forward through the range of dates:

让我们使用一个简单的for循环,LocalDate类,以及plusDays(1)方法,在日期范围内向前移动。

void iterateBetweenDatesJava8(LocalDate start, LocalDate end) {
    for (LocalDate date = start; date.isBefore(end); date = date.plusDays(1)) {
        processDate(date);
    }
}

It’s worth noting here that although the Stream API is available beginning in Java 8, iterating between two dates using the Date API in conjunction with the Stream API isn’t possible until Java 9.

这里值得注意的是,虽然从Java 8开始就有了Stream API,但是在Java 9之前,使用Date API与Stream API一起在两个日期之间进行迭代是不可能的

Please check out this article for a more detailed explanation of the Java 8 Date API.

请查看这篇文章,以了解对Java 8 Date API的更详细的解释。

4. Java 9+

4.JAVA 9+

Java 9 introduces the method datesUntil, which lets us use the Stream API to iterate from start date to end date.

Java 9引入了datesUntil,方法,让我们可以使用Stream API来从开始日期迭代到结束日期。

Let’s update our example code to take advantage of this feature:

让我们更新我们的示例代码,以利用这一特性。

void iterateBetweenDatesJava9(LocalDate start, LocalDate end) {
    start.datesUntil(end).forEach(this::processDate);
}

5. Conclusion

5.结论

As we’ve seen in this quick article, iterating over a range of dates is a simple task in Java. This is especially true when using Java 8 and later, where we can handle dates more easily with the Date API.

正如我们在这篇快速文章中所看到的,在Java中迭代一系列的日期是一项简单的任务。在使用Java 8及以后的版本时尤其如此,我们可以用Date API更容易地处理日期。

Note that in Java 7 and earlier, it’s recommended to deal with both date and time, even if we’re using only dates.

注意,在Java 7和更早的版本中,建议同时处理日期和时间,即使我们只使用日期。

However, with Java 8 and later, we have the advantage of choosing an appropriate class from the Date API like LocalDate, LocalDateTime, and other options, according to our needs.

然而,在Java 8及以后的版本中,我们可以根据自己的需要从Date API中选择一个合适的类,如LocalDate,LocalDateTime,和其他选项。

And of course, starting in Java 9, we can use the Stream API in conjunction with the Date API to iterate a stream of dates.

当然,从Java 9开始,我们可以结合使用Stream API和Date API来迭代一个日期流。

As always, the code snippets can be found over on GitHub.

像往常一样,代码片段可以在GitHub上找到over