Oct 01

The following custom iterator allows iteration through a date range.


import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;

public class DateIterator
   implements Iterator<Date>, Iterable<Date>
{

    private Calendar end = Calendar.getInstance();
    private Calendar current = Calendar.getInstance();

    public DateIterator(Date start, Date end)
    {
        this.end.setTime(end);
        this.end.add(Calendar.DATE, -1);
        this.current.setTime(start);
        this.current.add(Calendar.DATE, -1);
    }

    public boolean hasNext()
    {
        return !current.after(end);
    }

    public Date next()
    {
        current.add(Calendar.DATE, 1);
        return current.getTime();
    }

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

    public Iterator<Date> iterator()
    {
        return this;
    }

    public static void main(String[] args)
    {
    	Date d1 = new Date();
    	Calendar cal = Calendar.getInstance();
    	cal.add(Calendar.DATE, 20);
    	Date d2 = cal.getTime();

    	Iterator<Date> i = new DateIterator(d1, d2);
    	while(i.hasNext())
    	{
    		Date date = i.next();
    		System.out.println(date);
    	}
    }
} 

written by objects \\ tags: , ,


9 Responses to “How can I iterate through all dates in a range?”

  1. Nizar Says:

    Your class is very good thou i have noticed one thing that the loop iterates from the next value and printing an extra value, i have tested this to loop from 2009.12.03 to 20 days from that. it starts the iteration from 2009.12.04 till 2009.12.23 thou it should end on the 22nd

  2. objects Says:

    Have fixed that now, thanks for the feedback

  3. this dateiterator contains a bug i have fixed this bug Says:

    this is the new dateiterator enhanced

    import java.util.ArrayList;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.Iterator;
    import java.util.Map;

    /**
    *
    * @author mahmoud mourad
    */

    public class DateIterator implements Iterator, Iterable{

    private Calendar end = Calendar.getInstance();
    private Calendar current = Calendar.getInstance();

    public DateIterator(Date start, Date end)
    {
    this.end.setTime(end);
    this.end.add(Calendar.DATE, -1);
    this.current.setTime(start);
    this.current.add(Calendar.DATE, -1);
    }

    public boolean hasNext()
    {
    // return !current.after(end);
    boolean b1=false;
    boolean b2=false;
    boolean b3=false;
    boolean b=false;
    b1=current.get(Calendar.YEAR)>end.get(Calendar.YEAR);
    b2=current.get(Calendar.YEAR)==end.get(Calendar.YEAR);
    b3=current.get(Calendar.DAY_OF_YEAR)>end.get(Calendar.DAY_OF_YEAR);
    b=b1||(b2&&b3);
    return !b;
    }

    public Date next()
    {
    current.add(Calendar.DATE, 1);
    return current.getTime();
    }

    public void remove()
    {
    throw new UnsupportedOperationException(
    “Cannot remove”);
    }

    public Iterator iterator()
    {
    return this;
    }
    }

  4. Iddy Magohe Says:

    This Class is real helpful,especially when you are at the middle of coding and realize that you need date Iterator , this facility will pay a vital role, keep on the good Work

  5. Krish Says:

    I tried to use your daterange class.. I am unable to resolve with the error
    cannot find the variable theform in the program.
    Give me the full code of this program

  6. objects Says:

    Sorry about that, its fixed now

  7. Ilja Says:

    i tried to use it backwards
    and i cant!

  8. objects Says:

    It currently only works for iterating forwards.
    To have it support going backwards should just require checking if start is after end, and if it is decrement the date.

  9. Marcio Says:

    Thanks!

Leave a Reply