英文:
How to make a list of dates between two dates
问题
我正尝试创建一个包含在两个日期之间的日期列表。以下是我尝试的方法:
public void fillDates() {
long diffInMillis = Math.abs(secondDate.getTime() - firstDate.getTime());
long diff = TimeUnit.DAYS.convert(diffInMillis, TimeUnit.MILLISECONDS);
for (int i=0; i <= diff; i++) {
Calendar calendar = Calendar.getInstance();
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.DAY_OF_MONTH), calendar.get(Calendar.DAY_OF_MONTH)+i);
Long date = calendar.getTime().getTime();
String str = convertDate(date);
dates.add(str);
}
}
dates
是一个字符串列表,而 convertDate()
是将一个 long
型日期转换为字符串日期的函数。但我认为问题出现在上述代码中,因为每次都向列表中添加了相同的日期。
我知道还有其他类似的问题,但我没有找到真正有帮助的解决方法...但如果你对我有一个完全不同的解决方案,也请不要犹豫!我只是想要在用户通过“日期选择器”提交的两个日期之间获得一个(字符串)日期列表。
英文:
I'm trying to create a list of dates that are included between two dates. Here is how I tried to do it :
public void fillDates() {
long diffInMillis = Math.abs(secondDate.getTime() - firstDate.getTime());
long diff = TimeUnit.DAYS.convert(diffInMillis, TimeUnit.MILLISECONDS);
for (int i=0; i <= diff; i++) {
Calendar calendar = Calendar.getInstance();
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.DAY_OF_MONTH), calendar.get(Calendar.DAY_OF_MONTH)+i);
Long date = calendar.getTime().getTime();
String str = convertDate(date);
dates.add(str);
}
}
dates
is a list of strings, and convertDate()
is converting a long
date into a string date. But I think the problem comes from the lines above, as the same day is added to the list every time.
I know other similar questions exists, but I didn't find any that really helped me... But if you have an entirely different solution for me, don't hesitate ! I'm just trying to get a list of (string) dates between two dates that are submitted by the user through Date Pickers
.
答案1
得分: 1
你不应该使用 Calendar
、Date
或者 SimpleDateFormat
。它们会带来麻烦。 你最好使用 java.time
包中的类。
如果 firstDate
和 secondDate
都是 LocalDate
,那么会变得更加简单:
List<String> dateStrings = firstDate.datesUntil(secondDate.plusDays(1))
.map(date -> date.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")))
.collect(Collectors.toList());
需要注意的是,datesUntil
方法从 Java 9 开始存在。对于 Java 8,可以使用以下辅助方法代替:
public static Stream<LocalDate> datesUntil(LocalDate from, LocalDate toExclusive) {
long daysBetween = ChronoUnit.DAYS.between(from, toExclusive);
return Stream.iterate(from, t -> t.plusDays(1))
.limit(daysBetween);
}
英文:
You should not be using Calendar
, Date
or SmipleDateFormat
. They're troublesome. You are better off using classes from the java.time
package.
If both firstDate
and secondDate
are LocalDate
s, then it'll become much easier:
List<String> dateStrings = firstDate.datesUntil(secondDate.plusDays(1))
.map(date -> date.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")))
.collect(Collectors.toList());
Note that datesUntil
exists since Java 9. For Java 8, this is a helper method instead:
public static Stream<LocalDate> datesUntil(LocalDate from, LocalDate toExclusive) {
long daysBetween = ChronoUnit.DAYS.between(from, toExclusive);
return Stream.iterate(from, t -> t.plusDays(1))
.limit(daysBetween);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论