如何在两个日期之间创建一个日期列表

huangapple go评论74阅读模式
英文:

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 &lt;= 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

你不应该使用 CalendarDate 或者 SimpleDateFormat它们会带来麻烦。 你最好使用 java.time 包中的类。

如果 firstDatesecondDate 都是 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 LocalDates, then it'll become much easier:

List&lt;String&gt; dateStrings = firstDate.datesUntil(secondDate.plusDays(1))
    .map(date -&gt; date.format(DateTimeFormatter.ofPattern(&quot;yyyy-MM-dd&quot;)))
    .collect(Collectors.toList());

Note that datesUntil exists since Java 9. For Java 8, this is a helper method instead:

public static Stream&lt;LocalDate&gt; datesUntil(LocalDate from, LocalDate toExclusive) {
    long daysBetween = ChronoUnit.DAYS.between(from, toExclusive);
    return Stream.iterate(from, t -&gt; t.plusDays(1))
        .limit(daysBetween);
    }

huangapple
  • 本文由 发表于 2020年10月15日 04:32:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/64361083.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定