Java Day of Week (dow) program using Sakamoto & General Algorithm

Do you want to find the day of week (DOW) of your birth date ?. Find the java program here. Various algorithm are there to calculate the Day of week of a given date. As a week has 7 days , we can number the week days 1 to 7 or 0 to 6. If we number the week days from 0-6, then Sunday is 0 , Monday is 1 , … and Saturday is 6. These numbers we can get by calculating mod 7 of number of days(NODs) between starting date and the date which you want to calculate day of week. That means, fix a old known date (for eg. 14/09/1752 (dd/mm/yyyy) is Thursday). Suppose you want to calculate day of week of September 15th , 2011, then calculate the number of days between 15/09/2011 and 14/09/1752. Now calculate NODs Mod 7 which results 0 to 6. If the result is 0 means it is Sunday , …if the result is 6, then dow is Saturday.

Now let us see an general Algorithm to calculate Day of Week based on Gregorian calendar. We calculate Day of week using the following steps which simplifies the process. Consider a example date September 15th 2011, Now split the date into 4 parts like Century (20) , Year (11) , Month (09) and Days (15 )

1. Calculate the day of week of the century (20th) starts using the formula (dow_C=2*(3-(Century % 4))) where century is the first 2 digit of the year. The result of the formula gives the day of the week of January 0 of the the starting year (0) of the century, with plus one to the value when century mod 4 is 0. January 0 means 31st December of previous year.

2. Calculate the DOW of the year (11th) starts using the formula year=year+ (year/4) where year is last 2 digit of the year. Already we have the DOW of century starts. Now add this value with number of years and number of leap years passed since century starts which results the DOW on which the year starts.

3. Pick DOW of the month (September) starts from the table given below. The month January starts on the same day the year starts. The following table also shows how the value is calculated. The day of week for

i) January starts with 0 (for leap year 6)
ii) February starts with 3 (i.e. Number of days in January %7 => 31 % 7=3) (for leap year 2),
iii) March starts with 3 (Number of days in January + Number of days in February % 7 => (31 + 28 ) % 7 = 3)
iv) April Starts with 6 ( (31+28+31 ) %7 = 6)
v) May starts with 1 ((31+28+31+30 )%7 = 1)
vi) Similarly, Day of week of June – 4 , July – 6, August – 2 , September -5 , October – 0 , November -3 , December – 5

4. Add all the above values (output of step 1 + 2 + 3) with the Days of the date (15)

5. Now result of the 4th step mod 7, gives the day of week of the given date. if it is 0 then sunday , … if it is 6 then it is saturday.

Now Let us convert the above steps to Java program. The following code accepts the date only in dd/mm/yyyy through command prompt. The program has no validation for date. You can check the url to validate the date. Although in Java Gregorian calendar class is there to calculate day of week, the following code will be helpful to understand the logic behind the calculation of Day of week. The following program calculates day of week using above algorithm, Gregorian Calendar class and Sakamoto Algorithm.

```package com.javaonline;

import java.util.*;
class DOW
{
public static void main(String args[])
{
if (args.length!=1)
{
System.out.println("Error Input : Usage : java DOW dd/mm/yyyy ");
System.exit(0);
}
try
{
String dd=args.substring(0, 2 );
String mm=args.substring( 3,5 );
String yyyy=args.substring(6,10 );
System.out.println("Day of Week of  " + args + " is "+ dayofweek(dd,mm,yyyy));
System.out.println("Day of Week is using Calander Class " + args + " is " + dowUsingCalanderClass(dd,mm,yyyy));
System.out.println("Day of Week is using Sakamoto Algorithm " + args + " is " + dowUsingSakamotoAlg(dd,mm,yyyy));
}catch (Exception e)
{
System.out.println("Error Input : Usage : java DOW dd/mm/yyyy ");
}
}

private static String dayofweek(String dd, String mm, String yyyy)
{
int dow_month[]={0,3,3,6,1,4,6,2, 5,0,3,5};
int dow_month_leap[]={6,2};
String C=yyyy.substring(0,2); //century first 2 digit
String yy=yyyy.substring(2,4); // year last 2 digit
int yy2digit=Integer.valueOf(yy); // year last 2 digit integer
int month=Integer.valueOf(mm);;
int day=Integer.valueOf(dd);
int century=Integer.valueOf(C);
int dow_century=2*(3-(century % 4));
int dow_year= yy2digit + (yy2digit /4);
int sum=0;
if ((month==1 || month==2) && leap(Integer.valueOf(yyyy)))
{
//for month january & february in leap year
sum=dow_century + dow_year + dow_month_leap[month-1] + day;
}
else
{
sum=dow_century + dow_year + dow_month[month-1] + day;
}
int DOW=sum%7;
return weekday(DOW);
}

private static String dowUsingCalanderClass(String dd, String mm, String yyyy) {

Calendar GC = new GregorianCalendar();
GC.set(Integer.parseInt(yyyy) , Integer.parseInt(mm)-1, Integer.parseInt(dd) ,12,0,0);
int DOW = GC.get(Calendar.DAY_OF_WEEK); // 6=Friday
return weekday(DOW-1);
}

//Using Sakamoto Algorithm

private static String dowUsingSakamotoAlg(String dd, String mm, String yyyy)
{
int year=Integer.valueOf(yyyy);
int month=Integer.valueOf(mm);
int day=Integer.valueOf(dd);
int dow_month[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
year=year - (month<3 ? 1 : 0);
int dow =( year + year/4 - year/100 + year/400 + dow_month[month-1] + day) % 7;
return (weekday(dow));
}

// To map day of week numeric to day of week
private static String weekday(int DOW)
{
String dow="";
switch (DOW) {
case 0: dow = "Sunday"; break;
case 1: dow = "Monday"; break;
case 2: dow = "Tuesday"; break;
case 3: dow = "Wednesday"; break;
case 4: dow = "Thursday"; break;
case 5: dow = "Friday"; break;
case 6: dow = "Saturday"; break;
}
return dow;
}

//to check given year is leap or not
private static boolean leap(int yyyy)
{
return (yyyy % 4 ==0 && yyyy % 100!=0) || (yyyy % 400==0);
}

} ```

Running the above program with input date 14/09/1752 will give the below output: 