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

后端 未结 22 1836
余生分开走
余生分开走 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:53

    java.time Package

    If you are using Java 8, there is a much cleaner approach. The new java.time package in Java 8 incorporates the features of the Joda-Time API.

    Your requirement can be solved using the below code:

    String s = "2014-05-01";
    String e = "2014-05-10";
    LocalDate start = LocalDate.parse(s);
    LocalDate end = LocalDate.parse(e);
    List totalDates = new ArrayList<>();
    while (!start.isAfter(end)) {
        totalDates.add(start);
        start = start.plusDays(1);
    }
    

提交回复
热议问题