0

I'm trying to write an algorithm that allows me to determine what day of the week a given date is. I've seen on internet that I could import the java.util.calendar, but unfortunately I'm not allow to use that; instead I have to create the algorithm myself. This is what I wrote so far, but unfortunately it doesn't work. Any thoughts? Thanks

public class Date{

    private int year;
    private int month;
    private int day;


public String getDayOfTheWeek(){

        int JANUARY =1;
        int FEBRUARY =4;
        int MARCH =4;
        int APRIL =0;
        int MAY =2;
        int JUNE =5;
        int JULY =0;
        int AUGUST =3;
        int SEPTEMBER =6;
        int OCTOBER =1;
        int NOVEMBER =4;
        int DECEMBER =6;

        int lastTwoDigits = year % 100; 
        int lastTwoDigitsDivTwelve = lastTwoDigits/12; 
        int lastTwoDigitsRemainder = lastTwoDigits %12;
        int lastTwoDigitsRemainderDivFour = lastTwoDigitsRemainder/4;
        int addDayOfTheMonth = day;
        int finalMod = 7;


        return (lastTwoDigits + lastTwoDigitsDivTwelve + lastTwoDigitsRemainder + lastTwoDigitsRemainderDivFour + day + month) % finalMod ;
    }
}
Kushal
  • 8,100
  • 9
  • 63
  • 82
  • 2
    [Determination of the day of the week](http://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week), [How to Determine the Day of the Week](http://java.dzone.com/articles/algorithm-week-how-determine), [Zeller's Algorithm: Day of the Week](http://faculty.cs.niu.edu/~hutchins/csci230/zeller.htm) – MadProgrammer May 29 '15 at 04:49

2 Answers2

0

I believe your code may work, if you just make this slight change:

Change this line:

return (lastTwoDigits + lastTwoDigitsDivTwelve + lastTwoDigitsRemainder + lastTwoDigitsRemainderDivFour + day + month) % finalMod ;

to this line:

Integer.toString(return (lastTwoDigits + lastTwoDigitsDivTwelve + lastTwoDigitsRemainder + lastTwoDigitsRemainderDivFour + day + month) % finalMod);

However you might want to use the common mental calculation method. Here is a link to it's description on Wikipedia: Mental Calculation

It is the most readable form and I think very acceptable for your project. Here is a short description:

(d + m + y + (y/4) + c) % 7 where: d is day of the month, m is the month's number in the month's table, y is the last two digits of the year, and c is the century number.

The month table is the only information not given, and it is very easy to make, all you need to know is if the year is a leap year. That is also easily determinable using the following steps:

  1. If the year is evenly divisible by 4, go to step 2. Otherwise, go to step 5.
  2. If the year is evenly divisible by 100, go to step 3. Otherwise, go to step 4.
  3. If the year is evenly divisible by 400, go to step 4. Otherwise, go to step 5.
  4. The year is a leap year (it has 366 days).
  5. The year is not a leap year (it has 365 days).

Or in code,

if((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))

will return true if 'year' is a leap year.

Matt C
  • 4,470
  • 5
  • 26
  • 44
  • 1
    unfortunately my code doesn't work. I have the following error: incompatible types: int cannot be converted to java.lang.string – supergnagno May 29 '15 at 05:11
  • Change this line: `return (lastTwoDigits + lastTwoDigitsDivTwelve + lastTwoDigitsRemainder + lastTwoDigitsRemainderDivFour + day + month) % finalMod ;` to this line: `return Integer.toString(return (lastTwoDigits + lastTwoDigitsDivTwelve + lastTwoDigitsRemainder + lastTwoDigitsRemainderDivFour + day + month) % finalMod);` – Matt C May 29 '15 at 05:23
  • I've edited my post to contain the fix to your error. – Matt C May 29 '15 at 05:26
  • If add your string the compier returns the following: illegal start of expression..perhaps this works: return Integer.toString (lastTwoDigits + lastTwoDigitsDivTwelve + lastTwoDigitsRemainder + lastTwoDigitsRemainderDivFour + day + month) % finalMod); – supergnagno May 29 '15 at 05:33
0

http://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week

The tabular forerunner to Tøndering's algorithm is embodied in the following K&R C function.[10] With minor changes, it is adaptable to other high level programming languages such as APL2.[11] (A 6502 assembly language version exists as well.) Posted by Tomohiko Sakamoto on the comp.lang.c Usenet newsgroup in 1993, it is accurate for any Gregorian date:

//js
function dayofweek(y, m, d) 
    {
       t= [0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4];
        y -= m < 3;
        return Math.floor(y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;
    }

The function does not always preserve y, and returns 0 = Sunday, 1 = Monday, etc. In contrast, the following expression

lid
  • 204
  • 1
  • 6
  • Please consider adding some description to help OP understand what the code means/is doing. – Matt C May 29 '15 at 04:59