Java Calendar Day getCanonicalDayFrom(Calendar day, int daysFrom)

Here you can find the source of getCanonicalDayFrom(Calendar day, int daysFrom)

Description

Returns the canonical day of the day specified in days from another date

License

Apache License

Parameter

Parameter Description
daysFromToday today = 0, tomorrow = +1, yesterday = -1, same day next week = +7, etc

Declaration

public static Calendar getCanonicalDayFrom(Calendar day, int daysFrom) 

Method Source Code

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

import java.util.Calendar;

import java.util.GregorianCalendar;

public class Main {
    /**/*w  ww  . j  ava2 s .  c  om*/
     * Returns the canonical day of the day specified in days from another date
     * @param daysFromToday today = 0, tomorrow = +1, yesterday = -1, same day next week = +7, etc
     * @return
     */
    public static Calendar getCanonicalDayFrom(Calendar day, int daysFrom) {
        Calendar ret = getCanonicalDay(day);
        ret.add(Calendar.DAY_OF_YEAR, daysFrom);
        return getCanonicalDay(ret);
    }

    /**
     * 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. decreaseByDay(final Calendar calendar)
  2. endOfDay(Calendar calendar)
  3. getAge(Calendar dateOfBirth, Calendar onThisDay)
  4. getBeginDay(Calendar cal)
  5. getCanonicalDay(Calendar cal)
  6. getCleanDay(Calendar c)
  7. getCurDay(Calendar calendar)
  8. getDay(Calendar cal)
  9. getDay(Calendar calendar)