Question: public static int calculateWeekDay( int month, int year){ int weekdayOnFirstDayOfYear = calculateWeekdayOnFirstDayOfYear (year); int dayOfYear = calculateDayOfYearOnFirstOfMonth (month, year); //If Jan 1 falls on a
public static int calculateWeekDay(int month, int year){
int weekdayOnFirstDayOfYear = calculateWeekdayOnFirstDayOfYear(year);
int dayOfYear = calculateDayOfYearOnFirstOfMonth(month, year);
//If Jan 1 falls on a sunday, weekdayOnFirstDayOfYear = 0 for sunday
//and since Jan 1 is first day of year, its dayofyear = 1
//When the method calculateWeekDay is queried for Jan 1, this method should return 0 (for sunday)
//So, we need to do -1 on dayOfYear's value (1 - 2 + 0) % 7 = 0 % 7 = 0
int weekDay = (dayOfYear - 1 + weekdayOnFirstDayOfYear)%7;
if(DEBUG) System.out.println("In calculateWeekDay: weekday: " + weekDay);
return weekDay;
}
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
