12

I'm trying to implement without success a Date iterator with Joda time.
I need something that allows me to iterate all the days form startDate to endDate
Do you have any idea on how to do that?

2
  • 2
    This help at all? stackoverflow.com/questions/847520/… Commented Jul 23, 2009 at 22:48
  • 2
    If you want days, you want to look at the LocalDate class (set one as your start date) and do a plus days for each next. This will avoid the timezone issues inherent in instant. Commented Jul 23, 2009 at 23:37

3 Answers 3

28

Here's something to get you started. You may want to think about whether you want it to be inclusive or exclusive at the end, etc.

import org.joda.time.*;
import java.util.*;

class LocalDateRange implements Iterable<LocalDate>
{
    private final LocalDate start;
    private final LocalDate end;

    public LocalDateRange(LocalDate start,
                          LocalDate end)
    {
        this.start = start;
        this.end = end;
    }

    public Iterator<LocalDate> iterator()
    {
        return new LocalDateRangeIterator(start, end);
    }

    private static class LocalDateRangeIterator implements Iterator<LocalDate>
    {
        private LocalDate current;
        private final LocalDate end;

        private LocalDateRangeIterator(LocalDate start,
                                       LocalDate end)
        {
            this.current = start;
            this.end = end;
        }

        public boolean hasNext()
        {
            return current != null;
        }

        public LocalDate next()
        {
            if (current == null)
            {
                throw new NoSuchElementException();
            }
            LocalDate ret = current;
            current = current.plusDays(1);
            if (current.compareTo(end) > 0)
            {
                current = null;
            }
            return ret;
        }

        public void remove()
        {
            throw new UnsupportedOperationException();
        }
    }
}

class Test
{
    public static void main(String args[])
    {
        LocalDate start = new LocalDate(2009, 7, 20);
        LocalDate end = new LocalDate(2009, 8, 3);
        for (LocalDate date : new LocalDateRange(start, end))
        {
            System.out.println(date);
        }
    }
}

It's a while since I've written an iterator in Java, so I hope it's right. I think it's pretty much okay...

Oh for C# iterator blocks, that's all I can say...

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, this just made my life at least 900% easier.
I was googling for whether Joda has something to represent a "date range"... Apparently not, since you propose writing one's own LocalDateRange.
@Jonik Joda offers the Interval interface and its various implementation. However I was not able to find an Iterable implementation: maybe more complex...
1

http://code.google.com/p/google-rfc-2445 ?

Comments

1

I know you asked about Joda-Time. Today we should prefer to use java.time, the modern Java date and time API that is basically a further development of Joda-Time. Since Java 9 the iteration of a date range has been built in through a Stream:

    LocalDate startDate = LocalDate.of(2019, Month.AUGUST, 28);
    LocalDate endate = LocalDate.of(2019, Month.SEPTEMBER, 3);
    startDate.datesUntil(endate).forEach(System.out::println);

Output:

2019-08-28
2019-08-29
2019-08-30
2019-08-31
2019-09-01
2019-09-02

If you wanted the end date to be inclusive, use datesUntil(endate.plusDays(1)).

And if you literally wanted an Iterator:

    Iterator<LocalDate> ldi = startDate.datesUntil(endate).iterator();

The Joda-Time home page says:

Note that Joda-Time is considered to be a largely “finished” project. No major enhancements are planned. If using Java SE 8, please migrate to java.time (JSR-310).

(Joda-Time - Home)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.