Get the date of a weekday relative to a date, e.g. first Wednesday before 11-23

@author Hans-Peter Pfeiffer
@param date
@param weekday
@param number
@param count_itself
@return
975553
public String getWeekdayRelativeTo(String date, int weekday, int number, boolean count_itself) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		Calendar c = Calendar.getInstance();
				
		int day;
		int add;
		
		if(number == 0) {
			try{
				c.setTime(formatter.parse(date));
				date = formatter.format(c.getTime());
			} catch (ParseException e) {
				e.printStackTrace();
			}
			return date;			
		}
		else{
			
			if(number<0) {
				number+=1;
			}
						
			try{
				c.setTime(formatter.parse(date));
				day = c.get(Calendar.DAY_OF_WEEK);
				if((count_itself && number>0) || (!count_itself && number <= 0)) {
					if(day<=weekday) {
						add = weekday - day;
					}
					else{
						add = weekday - day + 7;
					}	
				}
				else{
					if(day<weekday) {
						add = weekday - day;
					}
					else{
						add = weekday - day + 7;
					}	
				}
				add += (( number - 1) * 7);
				c.add(Calendar.DAY_OF_MONTH, add);
				date = formatter.format(c.getTime());
			} catch (ParseException e) {
				e.printStackTrace();
			}
			return date;
		}
	}
683242
public static Date getFirstDayOfLastWeek(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(date.getTime() - 604800000L);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        Date lastWeekDate = c.getTime();

        c = new GregorianCalendar();
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.setTime(new Date(lastWeekDate.getTime()));
        c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); // Monday
        return c.getTime();
    }
806500
function monthCalendar(date) {
    var start = date.clone().startOf("month").startOf("week").startOf("day");
    var end = date.clone().endOf("month").endOf("week").startOf("day");
    var result = [];
    var current = start.weekday(0).subtract(1, "d");
    while (true) {
        current = current.clone().add(1, "d");
        result.push(current);
        if (areDatesEqual(current, end)) {
            break;
        }
    }
    return result;
}
1209365
@Nonnegative
  public static DayOfWeek getWeekday (@Nonnull final Weekday eWeekday)
  {
    switch (eWeekday)
    {
      case MONDAY:
        return DayOfWeek.MONDAY;
      case TUESDAY:
        return DayOfWeek.TUESDAY;
      case WEDNESDAY:
        return DayOfWeek.WEDNESDAY;
      case THURSDAY:
        return DayOfWeek.THURSDAY;
      case FRIDAY:
        return DayOfWeek.FRIDAY;
      case SATURDAY:
        return DayOfWeek.SATURDAY;
      case SUNDAY:
        return DayOfWeek.SUNDAY;
      default:
        throw new IllegalArgumentException ("Unknown weekday " + eWeekday);
    }
  }
1169114
public static int getDayOfWeek(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c.get(Calendar.DAY_OF_WEEK);
    }
543878
protected Calendar getCalendarInstance(final Date date, final boolean lenient) {
        Calendar cal = Dates.getCalendarInstance(date);
        // A week should have at least 4 days to be considered as such per RFC5545
        cal.setMinimalDaysInFirstWeek(4);
        cal.setFirstDayOfWeek(calendarWeekStartDay);
        cal.setLenient(lenient);
        cal.setTime(date);

        return cal;
    }
887318
public static Integer getRemainingDayOfMonth(Date date) {

        if (date == null)
            return null;

        // 月天数
        int days = getDayNumOfMonth(date);
        // 第几天
        int dayOfMonth = getDayOfMonth(date);

        return days - dayOfMonth;
    }
288797
public static Integer getRemainingDayOfSeason(Date date) {

        if (date == null)
            return null;

        return getDayNumOfSeason(date) - getDayOfSeason(date);
    }
1153307
public static Date plus(Date self, int days) {
        Calendar calendar = (Calendar) Calendar.getInstance().clone();
        calendar.setTime(self);
        calendar.add(Calendar.DAY_OF_YEAR, days);
        return calendar.getTime();
    }
1189933
public static int weekCount(Date start, Date end) {
		final Calendar startCalendar = Calendar.getInstance();
		startCalendar.setTime(start);
		final Calendar endCalendar = Calendar.getInstance();
		endCalendar.setTime(end);

		final int startWeekofYear = startCalendar.get(Calendar.WEEK_OF_YEAR);
		final int endWeekofYear = endCalendar.get(Calendar.WEEK_OF_YEAR);

		int count = endWeekofYear - startWeekofYear + 1;

		if (Calendar.SUNDAY != startCalendar.get(Calendar.DAY_OF_WEEK)) {
			count--;
		}

		return count;
	}
476536
int getDayOfWeek(Calendar calendar) {
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - firstDayOfWeekToDraw;
        dayOfWeek = dayOfWeek < 0 ? 7 + dayOfWeek: dayOfWeek;
        return dayOfWeek;
    }
1126487
public static Date getLastDayFromCurrentWeek(Date d) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(d);        
        // depends on Locale (if a week starts on Monday or on Sunday)
        if (cal.getFirstDayOfWeek() == Calendar.SUNDAY) {
        	cal.add(Calendar.WEEK_OF_YEAR, 1);  
        }	
        cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MILLISECOND, 999);
        return cal.getTime();
    }
927907
static DateValue nextWeekStart(DateValue d, Weekday wkst) {
        DTBuilder builder = new DTBuilder(d);
        builder.day += (7 - ((7 + (Weekday.valueOf(d).javaDayNum
                - wkst.javaDayNum)) % 7))
                % 7;
        return builder.toDate();
    }
354694
public static Integer getDayNumOfMonth(Date date) {

        if (date == null)
            return null;

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }
76258
public static int getDay(final Date date)
	{
		final Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.DAY_OF_MONTH);
	}
679429
public static Integer getDayNumOfSeason(Date date) {

        if (date == null)
            return null;

        int day = 0;
        Date[] seasonDates = getSeasonDate(date);
        for (Date d : seasonDates) {
            day += getDayOfMonth(d);
        }
        return day;
    }
583279
function getWeekNumber(day) {
  var date = clone(day);
  date.setHours(0, 0, 0);
  date.setDate(date.getDate() + 4 - (date.getDay() || 7));
  return Math.ceil(((date - new Date(date.getFullYear(), 0, 1)) / 8.64e7 + 1) / 7);
}