Java Utililty Methods Calendar Week

List of utility methods to do Calendar Week

Description

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

Method

intconvertIntToCalendarDayOfWeek(int dayOfWeek)
convert Int To Calendar Day Of Week
return dayOfWeek == Calendar.SUNDAY ? 7 : dayOfWeek - 1;
DateendOfWeek(Calendar calendar, Date date)
Adjusts the calendar to the end of the current week.
calendar.setTime(date);
endOfWeek(calendar);
return calendar.getTime();
CalendarfirstDayOfLastWeek(Calendar c)
first Day Of Last Week
c = (Calendar) c.clone();
c.add(Calendar.WEEK_OF_YEAR, -1);
c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek());
return c;
CalendargetADayOfWeek(Calendar day, int dayOfWeek)
get A Day Of Week
int week = day.get(Calendar.DAY_OF_WEEK);
if (week == dayOfWeek)
    return day;
int diffDay = dayOfWeek - week;
if (week == Calendar.SUNDAY) {
    diffDay -= 7;
} else if (dayOfWeek == Calendar.SUNDAY) {
    diffDay += 7;
...
intgetDateOfLastDayOfWeekInMonth(Calendar calendar, int dayOfWeek)
get Date Of Last Day Of Week In Month
int lastDateOfMonth = getLastDateOfMonth(calendar);
Calendar tmpCal = new GregorianCalendar(calendar.getTimeZone());
tmpCal.set(Calendar.YEAR, calendar.get(Calendar.YEAR));
tmpCal.set(Calendar.MONTH, calendar.get(Calendar.MONTH));
tmpCal.set(Calendar.DATE, lastDateOfMonth);
int day = tmpCal.get(Calendar.DAY_OF_WEEK);
if (day == dayOfWeek) {
    return tmpCal.get(Calendar.DATE);
...
CalendargetDayFromPreviousWeek(Calendar requestDate)
get Day From Previous Week
Calendar result = ((Calendar) requestDate.clone());
result.set(Calendar.DAY_OF_YEAR, (requestDate.get(Calendar.DAY_OF_YEAR) - 7));
return result;
intgetDayOfWeek(Calendar c)
get Day Of Week
switch (c.get(Calendar.DAY_OF_WEEK)) {
case Calendar.SUNDAY:
    return 0;
case Calendar.MONDAY:
    return 1;
case Calendar.TUESDAY:
    return 2;
case Calendar.WEDNESDAY:
...
intgetDayOfWeek(Calendar cal)
Quick hack to get day of week.
if (cal.get(Calendar.DAY_OF_WEEK) > 1) {
    return cal.get(Calendar.DAY_OF_WEEK) - 1;
return 7;
intgetDayOfWeek(Calendar date)
Returns the day of week depending if the week starts on sunday or monday.
int day = date.get(Calendar.DAY_OF_WEEK);
if (getFirstDayOfWeek() == Calendar.SUNDAY)
    return day - 1;
else {
    if (day >= Calendar.MONDAY)
        return day - Calendar.MONDAY;
    else
        return 6;
...
IntegergetDayOfWeekNumber(Calendar cal)
get Day Of Week Number
return cal.get(Calendar.DAY_OF_WEEK);