how to get a list of dates between two dates in java

后端 未结 22 1828
余生分开走
余生分开走 2020-11-22 13:24

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.

22条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-22 13:37

    Streams

    Edit: Joda-Time is now deprecated, changed the answer to use Java 8 instead.

    Here is the Java 8 way, using streams.

    List daysRange = Stream.iterate(startDate, date -> date.plusDays(1)).limit(numOfDays).collect(Collectors.toList());
    

提交回复
热议问题