Java Data Type How to - Process for a range of dates which are within the upper and lower limit set by 2 date variables








Question

We would like to know how to process for a range of dates which are within the upper and lower limit set by 2 date variables.

Answer

import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
// w  w w. jav  a2s  .co m
public class Main {
  public static void main(String[] args) {
    Date d1 = new Date();
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.DATE, 22);
    Date d2 = cal.getTime();

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

}

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

}

The code above generates the following result.