How can I iterate through all dates in a range?

Java 23.11.2013 No Comments

Ever wanted to iterate through a range of Date’s? We can use Iterator’s for looping through the elements of a Collection, but for Date’s we need to implement the Date arithmetic required.

The following class shows how we can create our own
Iterator implementation to easily iterate over a range of Date’s, one day at a time.

package com.learnjava.util;

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

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);
    }

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

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

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

    @Override
    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);
    	}
    }
}

Leave a Reply

s2Member®