Saturday, November 15, 2014

Determination of Day of the Week for a given Date

Input: Given a date in DD-MM-YYYY format, determine the day of the week for this date.

Input: 15-11-2014
Output: Saturday

Algorithm:

I. Assign 

0 to Sunday
1 to Monday
2 to Tuesday
3 to Wednesday
4 to Thursday
5 to Friday
6 to Saturday

II. 
The number of days which do not form a week are called odd days.
For example in a month of 30 days, 30/7 = 4 weeks and 2 odd days. [30 = 4*7+2]

A leap year has 366 days, odd days = 2
A non leap year has 365 days, odd days = 1

100 years have 5 odd days.
200 years have 3 odd days.
300 years have 1 odd days.
400 years have 0 odd days.

III.
Lets say we are given a date 31st March 1789.
First we find the number of odd days in 1788 years. Then we find the number of odd days from 1st Jan to 31st March of 1789. Add the results to get the total number of odd days. Next take oddDays % 7 to get the day of the week on 31st March 1789.

Given an year say 2014, try to break 2013 like

2013 = 400*5 + 13
         = 0*5 odd days + (3 leap years * 2 + 10 non-leap years * 1) odd days
         = 16 odd days

Now lets say we want to calculate the day for 4th March 2014

January has 31 days.
February has 29 days if the year is leap (2014 is not). So we take 28 days for February.

Total number of days before March 1, = 59

Current date = 4th
Total number of days = 59+4 = 63 days

Number of odd days in 63 days = 0 days

So total number of odd days = 16+0 = 16.
Day of the week = 16 % 7 = 2.

2 is for Tuesday.
The day is Tuesday.

Source:

public class DayofTheWeek {
    
    public static boolean isLeap(int year){
        if(year % 4 == 0){
            if(year % 100 == 0){
                return year%400==0; 
            }else return true;
        }else return false;
    }
    
    public static int getDays(int day, int month, int year){
        int days=0; 
        for(int i=1;i<month;++i){
            if(i==2){ if(isLeap(year)) days+= 29; else days+=28;}
            else if(i%2==0){ if(i<7)days += 30; else days+= 31;}
            else if(i%2==1){ if(i<=7)days += 31; else days+=30;}
        }
        days+=day;   
        return days;
    }
    
    public static int returnDay(int day, int month, int year){
        //Get number of days between the start of the year and the current date
        int daysInCurrentYear = getDays(day, month, year); 
        
        int oddDays = 0;
        year--;
        
        int n400s = year/400;
        
        if(n400s >= 1)
            year -= 400 * n400s; 
          
        if(year/300 == 1){
            year -= 300;
            oddDays += 1;
        }else if(year/200 == 1){
            year -= 200;
            oddDays += 3;
        }else if(year/100 > 0){
            year -= 100;
            oddDays += 5;
        } 
        
        int leapYears = year/4;
        int nonLeapYears = year-leapYears;
        
        oddDays += (leapYears * 2 + nonLeapYears);   
        oddDays += (daysInCurrentYear % 7);   
        
        return oddDays % 7;
    }
    
    public static void main(String[] args){   
        System.out.println(returnDay(4, 3, 2014));
    }
}

Output:
2

No comments:

Post a Comment