1. Overview
1.概述
The new Time API introduced in Java 8 made it possible to process date and time without using external libraries.
在Java 8中引入的新的时间API使处理日期和时间成为可能,无需使用外部库。
In this short tutorial, we will take a look at how getting all dates between two dates become easier over the time in different versions of Java.
在这个简短的教程中,我们将看看随着时间的推移,在不同版本的Java中,如何获得两个日期之间的所有日期变得更容易。
2. Using Java 7
2.使用Java 7
In Java 7, one way to calculate this is using the Calendar instance.
在Java 7中,计算的一种方法是使用Calendar实例。
First, we’ll get the start and end dates without time. Then, we’ll loop over these and add one day in each iteration using add method and Calendar.Date field, until it reaches the end date.
首先,我们将得到开始和结束不含时间的日期。然后,我们将循环这些日期,并在每次迭代中使用add方法和Calendar.Date字段添加一天,直到到达结束日期。
Here is the code demonstrating it – using Calendar instance:
下面是演示的代码–使用Calendar实例。
public static List getDatesBetweenUsingJava7(Date startDate, Date endDate) {
List datesInRange = new ArrayList<>();
Calendar calendar = getCalendarWithoutTime(startDate);
Calendar endCalendar = getCalendarWithoutTime(endDate);
while (calendar.before(endCalendar)) {
Date result = calendar.getTime();
datesInRange.add(result);
calendar.add(Calendar.DATE, 1);
}
return datesInRange;
}
private static Calendar getCalendarWithoutTime(Date date) {
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
calendar.set(Calendar.HOUR, 0);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar;
}
3. Using Java 8
3.使用Java 8
In Java 8, we can now create a continuous infinite Stream of dates and take only the relevant part. Unfortunately, there is no way of terminating an infinite Stream when a predicate gets matched – this is why we need to calculate the number of days between those two days and then simply limit() the Stream:
在Java 8中,我们现在可以创建一个连续的无限的Stream日期,并且只取相关部分。不幸的是,当一个谓词被匹配时,没有办法终止一个无限的Stream–这就是为什么我们需要计算这两天之间的天数,然后简单地limit()这个Stream:。
public static List<LocalDate> getDatesBetweenUsingJava8(
LocalDate startDate, LocalDate endDate) {
long numOfDaysBetween = ChronoUnit.DAYS.between(startDate, endDate);
return IntStream.iterate(0, i -> i + 1)
.limit(numOfDaysBetween)
.mapToObj(i -> startDate.plusDays(i))
.collect(Collectors.toList());
}
Notice how, first, we can get the difference of days between two dates using the between function – associated with DAYS constant of ChronoUnit enumeration.
请注意,首先,我们可以使用between函数获得两个日期之间的天数差–与ChronoUnit枚举的DAYS常数相关。
Then we create a Stream of integers representing the number of days since the starting date. In the next step, we convert our integers to LocalDate objects using the plusDays() API.
然后我们创建一个Stream的整数,代表自起始日期以来的天数。在下一步,我们使用plusDays() API将我们的整数转换为LocalDate对象。
Finally, we collect everything into a list instance.
最后,我们把所有东西都收集到一个列表实例中。
4. Using Java 9
4.使用Java 9
Finally, Java 9 brings dedicated methods for calculating this:
最后,Java 9带来了专门的方法来计算这个问题。
public static List<LocalDate> getDatesBetweenUsingJava9(
LocalDate startDate, LocalDate endDate) {
return startDate.datesUntil(endDate)
.collect(Collectors.toList());
}
We can get the dates between two dates with single method call using the dedicated datesUntil method of a LocalDate class. The datesUntill returns the sequentially ordered Stream of dates starting from the date object whose method is called to the date given as method argument.
我们可以通过使用LocalDate类的专用datesUntil方法来获得两个日期之间的日期。datesUntill返回按顺序排列的Stream日期,从方法被调用的日期对象开始到作为方法参数的日期。
5. Conclusion
5.结论
In this quick article, we looked at how can we get all dates between two dates using the different versions of Java.
在这篇快速文章中,我们研究了如何使用不同版本的Java获得两个日期之间的所有日期。
We discussed how Time API introduced in Java 8 release made it easier to run operations over date literals and in Java 9, it can be done by just calling datesUntil.
我们讨论了在Java 8版本中引入的时间API是如何使对日期字头的操作更容易进行的,在Java 9中,只需调用datesUntil.就可以完成。
And, as always, the code snippets can be found over on GitHub.
而且,像往常一样,可以在GitHub上找到代码片段。