How to iterate through range of Dates in Java ?

How to iterate through range of Dates in Java ?

Asked on December 18, 2018 in Java.
Add Comment


  • 5 Answer(s)

    We can solve this problem by using java 8’s time-API, for this problem particularly java.time.LocalDate or equivalent Joda Time classes for Java 7 and older versions.

    for (LocalDate date = startDate; date.isBefore(endDate); date = date.plusDays(1))
    {
    ...
    }
    

    There is always better to use java.time or Joda Time over the built-in two classes known as  Date/Calendar .

    Answered on December 18, 2018.
    Add Comment

    Try this solution, this should works.

    Using the java.time classes in Java 8:

    // Monday, February 29 is a leap day in 2016 (otherwise, February only has 28 days)
    LocalDate start = LocalDate.parse("2016-02-28"),
            end = LocalDate.parse("2016-03-02");
    // 4 days between (end is inclusive in this example)
    Stream.iterate(start, date -> date.plusDays(1))
            .limit(ChronoUnit.DAYS.between(start, end) + 1)
            .forEach(System.out::println);
    

    Output is here:

    2016-02-28
    2016-02-29
    2016-03-01
    2016-03-02
    

    Or we can use this code:

    LocalDate next = start.minusDays(1);
    while ((next = next.plusDays(1)).isBefore(end.plusDays(1))) {
        System.out.println(next);
    }
    

    Added the datesUntil() method Java 9:

    start.datesUntil(end.plusDays(1)).forEach(System.out::println);
    
    Answered on December 18, 2018.
    Add Comment

    Here the query is solved by using the while loop,

    Calendar start = Calendar.getInstance();
    start.setTime(startDate);
     
    Calendar end = Calendar.getInstance();
    end.setTime(endDate);
     
    while( !start.after(end)){
    Date targetDay = start.getTime();
    // Do Work Here
     
    start.add(Calendar.DATE, 1);
    }
    
    Answered on December 18, 2018.
    Add Comment

    This is essentially the same answer BalusC gave, but a bit more readable with a while loop in place of a for loop:

    Calendar start = Calendar.getInstance();
    start.setTime(startDate);
    
    Calendar end = Calendar.getInstance();
    end.setTime(endDate);
    
    while( !start.after(end)){
        Date targetDay = start.getTime();
        // Do Work Here
    
        start.add(Calendar.DATE, 1);
    }
    Answered on January 17, 2019.
    Add Comment

    Here is Java 8 code. I think this code will solve your problem.Happy Coding

        LocalDate start = LocalDate.now();
        LocalDate end = LocalDate.of(2016, 9, 1);//JAVA 9 release date
        Long duration = start.until(end, ChronoUnit.DAYS);
        System.out.println(duration);
         // Do Any stuff Here there after
        IntStream.iterate(0, i -> i + 1)
                 .limit(duration)
                 .forEach((i) -> {});
         //old way of iteration
        for (int i = 0; i < duration; i++)
         System.out.print("" + i);// Do Any stuff Here
    Answered on January 17, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.