Tag Archives: date

Day on a given date

calendarIt’s possible to find the day on a given date using a Java API method or a formula, but it is also possible to determine the day on any given date from the basic principles. The logic essentially consists of finding out the number of days from some reference point and then finding out the remainder of number of days divided by 7. Remainder of a number divided by 7 would always be in the range of 0-6. If the remainder is 0 the days would be same as that on the reference date, if the remainder is 1 the day would be one more than the day on the reference date and so on.

In order to find the day on any given date our reference date would be January 1, 1900 and  January 1, 1900 was Monday. We are assuming that the date for we are trying to find the date would be on or after out reference date.

I have already discussed how to find the duration between any two dates in my earlier post but in this post we would look at another method which is simple and slightly inefficient. The method is to keep on increasing the initial date by one till its equal to the final date. At each increment of the initial date we would increase out counter by one. When the initial date is equal to the final date the value of the counter would give the duration between the two dates.

Determination of leap year is a very important aspect in these type of programs and contrary to the popular belief leap years are not the years which are completely divisible by 4. Those who are working on UNIX or Linux can easily verify the same by typing cal 2 1900 in the terminal window. The command will display the second month (February) of the year 1900 and one can see that the number of days in February 1900 is 28 and not 29! Those who are using an Operating System which doesn’t have the cal command available can visit this URL to confirm the same.

The correct method to test whether any given year is a leap or not is to check for divisibility by 400 in case the year is divisible by 100 and to check for divisibility by 4 in case the year is not divisible by 100.

Using the above method the year 1900 should be checked for divisibility by 400 since year 1900 is divisible by 100 and the year 2000 should be checked for divisibility by 4 since year 2000 is not divisible by 100. Most of us do not know this rule because we usually deal in recent past and recent future. The last special case was the year 1900 which passed a long time back, the next special case would be 2100 which is almost 85 years away!

To reduce the number of if-statements in our program we would use a string array to store the days. The complete program would be as follows:

class Calendar{
    public static boolean isLeap(int year){
        int divisor=year%100==0?400:4;
        return year%divisor==0;
    }
    public static int differenceBetweenDates(int d1, int m1, int y1, int d2, int m2, int y2){
        int[] days={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        days[1]=isLeap(y1)?29:28;
        int duration=0;
        while(!(d1==d2 && m1==m2 && y1==y2)){
            d1++;
            duration++;
            if(d1>days[m1-1]){
                d1=1;
                m1++;
                if(m1>12){
                    m1=1;
                    y1++;
                    if(isLeap(y1)) days[1]=29;
                    else days[1]=28;
                }
            }
        }
        return duration;
    }
    public static String dayOnDate( int day, int month, int year ){
        String days[]={ "Monday","Tuesday","Wednesday","Thursday","Friday","Saturday", "Sunday" };
        int count = differenceBetweenDates( 1, 1, 1900, day, month, year );
        System.out.println( count );
        return days[ count % 7 ];
    }
    public static void main( String []args ){
        System.out.println( dayOnDate( 9,3,2014 ) );
    }
}

The above Java program to find the day on any date is pretty self-explanatory. The reader should notice that our string array days is starting from Monday because our reference day was on Monday. As usual, the main function has purposely been kept to minimum and its expected that students will write appropriate input/output statements as per the requirement.