0

This question arose when I was trying to understand Sakamoto's algorithm for finding the day of a given date.

I found the working of the algorithm to be difficult to comprehend even after reading the following Stackoverflow answer

So, I decided to first solve a specific problem of finding the day in which a given year begins( Jan-1).

From the Sakamoto's algorithm, I just took the part of adding the additional days contributed by the leap and non-leap years. My code is as follows:

public String getDay(String date)
{
    String[] days = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };

    int day = Integer.parseInt(date.split("/")[0]);
    int month = Integer.parseInt(date.split("/")[1]);
    int year = Integer.parseInt(date.split("/")[2]);

    year--;     // to calculate the additional days till the previous year

    int dayOfTheWeek = (year + year/4 - year/100 + year/400) % 7;

    return days[dayOfTheWeek];
}

Thus, for the date "1/1/0001", it returns Sunday.

To verify its correctness, I implemented Sakamoto's algorithm and compared the results and my program's result always seems to be one day before the day returned by the Sakamoto's algorithm.

For the date "1/1/0001" my program returns Sunday, while Sakamoto's returns Monday.

So,

1) Does it mean that the Gregorian calendar started on Monday instead of Sunday??

2) If yes, does it mean I should add 1 to the result to get the right day or is my program logically incorrect?

Finally, I used TimeAndDate site's day calculator tool and "1/1/0001" starts on Saturday.

My final question is

3) On what day does the Gregorian calendar start?

Any light on the these questions is much appreciated.

Thanks,

Community
  • 1
  • 1
Balasubramanian
  • 899
  • 3
  • 9
  • 16

1 Answers1

0

What exactly is the point of reinventing the wheel?

Joda-Time is a de facto standard for date-time operations in Java, and it provides dayOfWeek method for its DateTime objects. See e.g. http://joda-time.sourceforge.net/userguide.html#Querying_DateTimes

If you are then still interested in details how to get the computation right, see https://github.com/JodaOrg/joda-time/blob/master/src/main/java/org/joda/time/chrono/BasicChronology.java#L538

Oleg Sklyar
  • 9,834
  • 6
  • 39
  • 62
  • I am doing it as a learning exercise , to understand what happens behind the screens. Moreover, my question is not specific to any particular language, it is on the algorithm itself. – Balasubramanian May 21 '14 at 15:13