如何获取Java中两个日期之间的日期列表
问题描述:
我想在开始日期和结束日期之间列出日期。
I want a list of dates between start date and end date.
结果应该是包括开始和结束日期在内的所有日期的列表。
The result should be a list of all dates including the start and end date.
答
我建议使用 Joda-Time 。一次添加一天直到结束日期。
I suggest to use Joda-Time for that. Add one day at a time until reaching the end date.
int days = Days.daysBetween(startDate, endDate).getDays();
List<LocalDate> dates = new ArrayList<LocalDate>(days); // Set initial capacity to `days`.
for (int i=0; i < days; i++) {
LocalDate d = startDate.withFieldAdded(DurationFieldType.days(), i);
dates.add(d);
}
将自己的迭代器实现为嗯,那会更好。
It wouldn't be too hard to implement your own iterator to do this as well, that would be even nicer.