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



December 2nd, 2009 at 11:08 pm
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
December 4th, 2009 at 9:52 am
Have fixed that now, thanks for the feedback
May 1st, 2010 at 10:30 pm
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;
}
}
May 30th, 2011 at 1:27 am
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
July 8th, 2011 at 1:34 pm
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
July 8th, 2011 at 1:39 pm
Sorry about that, its fixed now
August 4th, 2011 at 6:44 pm
i tried to use it backwards
and i cant!
August 4th, 2011 at 6:49 pm
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.
November 4th, 2011 at 10:51 am
Thanks!