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

intlastDayOfWeek(Calendar cal)
Get the last day of the week respecting the first day of the week for the provided Calendar.
final int firstDayOfWeek = cal.getFirstDayOfWeek();
final int lastDayOfWeek;
if (firstDayOfWeek != 1) {
    lastDayOfWeek = firstDayOfWeek - 1; 
} else {
    lastDayOfWeek = cal.getActualMaximum(Calendar.DAY_OF_WEEK); 
return lastDayOfWeek;
...
CalendarlastDayOfWeek(Calendar calendar)
last Day Of Week
Calendar cal = (Calendar) calendar.clone();
int day = cal.get(Calendar.DAY_OF_YEAR);
while (cal.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY) {
    cal.set(Calendar.DAY_OF_YEAR, ++day);
return cal;
voidsetStartOfWeek(Calendar date)
Sets the date to the begin of the week.
date.add(Calendar.DATE, -getDayOfWeek(date));
date.set(Calendar.HOUR_OF_DAY, 0);
date.set(Calendar.MINUTE, 0);
date.set(Calendar.SECOND, 0);
date.set(Calendar.MILLISECOND, 0);
voidsetTimeAsFirstDayOfWeek(Calendar calendar)
set Time As First Day Of Week
int monthBeforeSet = calendar.get(Calendar.MONTH);
calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek());
int monthAfterSet = calendar.get(Calendar.MONTH);
if (monthBeforeSet == Calendar.JANUARY && monthAfterSet == Calendar.DECEMBER) {
    calendar.setMinimalDaysInFirstWeek(calculateMinimalDaysInFirstWeek(calendar));
normalise(calendar);
GregorianCalendartruncateToWeek(GregorianCalendar date)
Truncate the given GregorianCalendar date to the nearest week.
GregorianCalendar result = (GregorianCalendar) date.clone();
switch (result.get(Calendar.DAY_OF_WEEK)) {
case Calendar.TUESDAY:
    result.add(Calendar.DAY_OF_MONTH, -1);
    break;
case Calendar.WEDNESDAY:
    result.add(Calendar.DAY_OF_MONTH, -2);
    break;
...
intweekDay(Calendar date)
week Day
return date.get(Calendar.DAY_OF_WEEK);
voidweekFloor(Calendar calendar)
week Floor
int i = calendar.get(Calendar.YEAR);
int k = calendar.get(Calendar.DAY_OF_YEAR);
int l = calendar.get(Calendar.DAY_OF_WEEK);
int i1 = calendar.getFirstDayOfWeek();
int j1 = l - i1;
if (j1 > 0)
    k -= j1;
else if (j1 < 0)
...
intweekFromBinStart(Calendar date)
week From Bin Start
return numberOfDaysBetween(startDateForBinning, date) / 7;