Java 8 LocalDate - How do I get all dates between two dates?

前端 未结 8 1988
陌清茗
陌清茗 2020-12-03 00:10

Is there a usablility to get all dates between two dates in the new java.time API?

Let\'s say I have this part of code:



        
8条回答
  •  爱一瞬间的悲伤
    2020-12-03 01:00

    In my time library Time4J, I have written an optimized spliterator to construct a stream of calendar dates with good parallelization characteristics. Adapted to your use-case:

    LocalDate start = ...;
    LocalDate end = ...;
    
    Stream stream = 
      DateInterval.between(start, end) // closed interval, else use .withOpenEnd()
        .streamDaily()
        .map(PlainDate::toTemporalAccessor);
    

    This short approach can be an interesting start point if you are also interested in related features like clock intervals per calendar date (partitioned streams) or other interval features and want to avoid awkward hand-written code, see also the API of DateInterval.

提交回复
热议问题