Java Utililty Methods Calendar Day

List of utility methods to do Calendar Day

Description

The list of methods to do Calendar Day are organized into topic(s).

Method

intdate2day(Calendar cal)
dateday
return cal.get(Calendar.YEAR) * 10000 + (cal.get(Calendar.MONTH) + 1) * 100
        + cal.get(Calendar.DAY_OF_MONTH);
intday(Calendar calendar)
day
return calendar.get(Calendar.DAY_OF_MONTH);
intday(Calendar date)
day
return date.get(Calendar.DAY_OF_MONTH);
Calendarday2date(int day, Calendar cal)
daydate
cal.clear();
cal.set(Calendar.YEAR, day / 10000);
cal.set(Calendar.MONTH, (day % 10000) / 100 - 1);
cal.set(Calendar.DAY_OF_MONTH, day % 100);
return cal;
CalendardayBegin(Calendar cal)
Truncates time to zero, keeps date.
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal;
voiddayBegin(Calendar calendar)
day Begin
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
intdayDistance(Calendar date1, Calendar date2)
day Distance
int noDays = 0;
if (date1.before(date2)) {
    Calendar temp = Calendar.getInstance();
    temp.setTimeInMillis(date1.getTimeInMillis());
    while (temp.before(date2)) {
        temp.add(Calendar.DATE, 1);
        noDays++;
} else if (date1.after(date2)) {
    Calendar temp = Calendar.getInstance();
    temp.setTimeInMillis(date2.getTimeInMillis());
    while (temp.before(date1)) {
        temp.add(Calendar.DATE, 1);
        noDays++;
return noDays;
CalendardayEndTime(Calendar calendar)
Changes input calendar by resetting time fields to the end of day.
calendar.set(Calendar.HOUR, calendar.getMaximum(Calendar.HOUR));
calendar.set(Calendar.MINUTE, calendar.getMaximum(Calendar.MINUTE));
calendar.set(Calendar.SECOND, calendar.getMaximum(Calendar.SECOND));
calendar.set(Calendar.MILLISECOND, calendar.getMaximum(Calendar.MILLISECOND));
return calendar;
voiddayFloor(Calendar calendar)
day Floor
hourFloor(calendar);
calendar.set(Calendar.HOUR_OF_DAY, 0);
IterabledayIterable(Calendar from, Calendar to)
returns an interable of canonical days in (inclusive) range of days
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() {
        };
};