Java Utililty Methods Day of Week

List of utility methods to do Day of Week

Description

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

Method

DategetFirstDayOfWeek(int year, int week)
get First Day Of Week
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, Calendar.JANUARY);
cal.set(Calendar.DATE, 1);
Calendar c = (Calendar) cal.clone();
c.add(Calendar.DATE, week * 7);
return getStartTimeOfWeek(c.getTime());
DategetFirstDayOfWeek(int year, int week)
get First Day Of Week
Calendar c = new GregorianCalendar();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, Calendar.JANUARY);
c.set(Calendar.DATE, 1);
Calendar cal = (GregorianCalendar) c.clone();
cal.add(Calendar.DATE, week * 7);
return cal.getTime();
Date[]getFirstDayWeekly(final int year)
Returns an array of Dates starting with the first day of each week of the year.
final GregorianCalendar cal = gregorianCalendarThreadLocal.get();
final GregorianCalendar testCal = new GregorianCalendar();
List<Date> dates = new ArrayList<>();
cal.setTime(trimDate(cal.getTime()));
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, 0);
cal.set(Calendar.WEEK_OF_YEAR, 1);
cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
...
DategetFirstMonthWeek(Date month)
Gets the first week which ends in specified month.
Calendar firstWeek = Calendar.getInstance();
firstWeek.setTime(month);
firstWeek.set(Calendar.DATE, 1);
firstWeek.set(Calendar.HOUR, 0);
firstWeek.set(Calendar.MINUTE, 0);
firstWeek.set(Calendar.SECOND, 0);
firstWeek.set(Calendar.MILLISECOND, 0);
firstWeek.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
...
DategetFirstWeekdayBetween(Date fromDate, Date toDate, int dayOfWeek)
get First Weekday Between
Calendar aDay = Calendar.getInstance();
aDay.setTime(fromDate);
while (aDay.getTime().before(toDate)) {
    if (aDay.get(Calendar.DAY_OF_WEEK) == dayOfWeek) {
        return aDay.getTime();
    aDay.add(Calendar.DATE, 1);
return null;
intgetFirstWeekofMonth(Date date1)
get First Weekof Month
Calendar cld = Calendar.getInstance();
cld.setTime(date1);
cld.set(5, 1);
int week = cld.get(3);
return week;
intgetIntervalIncludeWeekend(Date date)
get Interval Include Weekend
Calendar now = Calendar.getInstance();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int d = (int) ((now.getTimeInMillis() - calendar.getTimeInMillis()) / (24 * 60 * 60 * 1000));
return d;
intgetISO8601WeekOfYear(Date date)
Computes the ISO-8601 week of year for the given date See: http://stackoverflow.com/questions/147178/what-is-a-good-simple-way-to-compute-iso-8601-week-number
Calendar calendar = Calendar.getInstance();
calendar.setMinimalDaysInFirstWeek(4);
calendar.setFirstDayOfWeek(Calendar.MONDAY);
calendar.setTime(date);
return calendar.get(Calendar.WEEK_OF_YEAR);
intgetIsoDayOfWeek(Date date)
Return the day of week.
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(date.getTime());
int val = cal.get(Calendar.DAY_OF_WEEK) - 1;
return val == 0 ? 7 : val;
intgetIsoWeek(java.util.Date date)
Returns the week of the year according to the ISO 8601 specification.
Calendar c = Calendar.getInstance();
c.setTimeInMillis(date.getTime());
c.setFirstDayOfWeek(Calendar.MONDAY);
c.setMinimalDaysInFirstWeek(4);
return c.get(Calendar.WEEK_OF_YEAR);