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

ListgetWeekDayOfYear(Date date)
get Week Day Of Year
Calendar calendar = getCalendarFromDate(date);
calendar.setFirstDayOfWeek(Calendar.MONDAY);
calendar.setMinimalDaysInFirstWeek(DAYS_OF_A_WEEK);
int week = calendar.get(Calendar.WEEK_OF_YEAR);
int year = calendar.get(Calendar.YEAR);
List<Date> result = new ArrayList<Date>();
result.add(getDateOfYearWeek(year, week, Calendar.MONDAY));
result.add(getDateOfYearWeek(year, week, Calendar.TUESDAY));
...
StringgetWeekDays(String _date, int _amount)
get Week Days
String res;
int year = Integer.parseInt(_date.substring(0, 4));
int month = Integer.parseInt(_date.substring(4, 6));
int day = Integer.parseInt(_date.substring(6, 8));
Calendar wCal = Calendar.getInstance();
wCal.set(year, month - 1, day);
wCal.add(Calendar.DATE, _amount);
year = wCal.get(Calendar.YEAR);
...
intgetWeekDaysBetween(Date fromDate, Date toDate, int dayOfWeek)
get Week Days Between
int result = 0;
Date firstDate = getFirstWeekdayBetween(fromDate, toDate, dayOfWeek);
if (firstDate == null) {
    return 0;
Calendar aDay = Calendar.getInstance();
aDay.setTime(firstDate);
while (aDay.getTime().before(toDate)) {
...
DategetWeekEnd(Date date, int w)
get Week End
Calendar c = Calendar.getInstance();
c.setTime(date);
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
if (dayOfWeek == 0) {
    dayOfWeek = 7;
c.add(Calendar.DATE, -dayOfWeek + 7);
c.add(Calendar.WEEK_OF_MONTH, w);
...
DategetWeekFirstDay(Date weekday)
get Week First Day
Calendar cal = Calendar.getInstance();
cal.setTime(weekday);
if (cal.get(cal.DAY_OF_WEEK) == 1) {
    cal.add(cal.DATE, -6);
} else {
    cal.add(cal.DATE, 2 - cal.get(cal.DAY_OF_WEEK));
return cal.getTime();
...
StringgetWeekID(Date dateValue)
get Week ID
String strWeekID = "";
Calendar objCalendar = Calendar.getInstance();
objCalendar.setTime(dateValue);
int iYear = objCalendar.get(Calendar.YEAR);
int iWeek = objCalendar.get(Calendar.WEEK_OF_YEAR);
if (iWeek >= 10) {
    strWeekID = iYear + String.valueOf(iWeek);
} else {
...
StringgetWeekNum(Date date)
get Week Num
Calendar c = Calendar.getInstance(Locale.CHINA);
c.setTime(date);
String week = Integer.toString(c.get(Calendar.WEEK_OF_YEAR));
return week;
longgetWeekNumber(Date date)
get Week Number
long retVal = 0;
if (date != null) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
        calendar.add(Calendar.DAY_OF_YEAR, -1);
    calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
...
IntegergetWeekNumber(Date date, Integer day)
get Week Number
Calendar c = Calendar.getInstance();
c.setTime(date);
return c.get(Calendar.WEEK_OF_MONTH) + day;
intgetWeekNumberFromDate(Date date)
Compute the week number of parameter date
if (date != null) {
    Calendar calendar = Calendar.getInstance();
    calendar.setFirstDayOfWeek(Calendar.MONDAY);
    calendar.setTime(date);
    return calendar.get(Calendar.WEEK_OF_YEAR);
} else {
    return 0;