Java Calendar Day dayIterable(Calendar from, Calendar to)

Here you can find the source of dayIterable(Calendar from, Calendar to)

Description

returns an interable of canonical days in (inclusive) range of days

License

Apache License

Parameter

Parameter Description
from a parameter
to a parameter

Declaration

public static Iterable<Calendar> dayIterable(Calendar from, Calendar to) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.Calendar;

import java.util.GregorianCalendar;
import java.util.Iterator;

public class Main {
    /**// w  ww.j a  v a2  s  . c  om
     * returns an interable of canonical days in (inclusive) range of days
     * @param from
     * @param to
     * @return
     */
    public static Iterable<Calendar> dayIterable(Calendar from, Calendar to) {
        final Calendar nextDay = getCanonicalDay(from);
        final Calendar upto = getCanonicalDay(to);
        return new Iterable<Calendar>() {
            public Iterator<Calendar> iterator() {
                return new Iterator<Calendar>() {
                    public boolean hasNext() {
                        return !nextDay.after(upto);
                    }

                    public Calendar next() {
                        Calendar ret = getCanonicalDay(nextDay);
                        nextDay.add(Calendar.DAY_OF_YEAR, 1);
                        return ret;
                    }

                    public void remove() {
                        //do nothing
                    }
                };
            }
        };
    }

    /**
     * Returns the canonical day calendar for a given calendar, which is the first millisecond of 
     * the day (2008/03/07 15:23:32 992ms --> 2008/03/07 0:0:0 0ms)
     * @param cal
     * @return 
     */
    public static Calendar getCanonicalDay(Calendar cal) {
        Calendar ret = new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
                cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        ret.set(Calendar.MILLISECOND, 0);
        ret.setTimeZone(cal.getTimeZone());
        return ret;
    }
}

Related

  1. dayBegin(Calendar cal)
  2. dayBegin(Calendar calendar)
  3. dayDistance(Calendar date1, Calendar date2)
  4. dayEndTime(Calendar calendar)
  5. dayFloor(Calendar calendar)
  6. dayMarkToCalendar(int day)
  7. dayPrecision(Calendar calendar)
  8. daysFromBinStart(Calendar date)
  9. daysSinceTheJesus(Calendar cal)