# Find Day of The Week Using Any Given Date

Harry Peters

Ranch Hand

Posts: 39

posted 5 months ago

I have a massive project due Nov. 7th but anyway I'm at a section where we are supposed to have a method: getDayOfTheWeek(). We are supposed to use an algorithm and our instructor has this page to show us:

So this is tricky and I'm wondering if someone can help me start this. Seems like it starts off using modulus. Maybe somehow using a code that looks at the last 2 digits of a year but I'm not sure. How should I start this?

*Here is the algorithm to determine what day of the week a given date is:*

Example dates: August 16, 1989 and March 20, 1950

Step 1: Only look at the last two digits of the year and determine how many 12s fit in it

7 12s in 89, 4 12s in 50

Step 2: Look at the remainder of this division:

89 – 7 * 12 = 5, 50 – 4 * 12 = 2

Step 3: How many 4s fit into that remainder:

1 four in 5 0 fours in 2

Step 4: Add the day of the month:

16 for the 16th 20 for the 20th

Step 5: Add the month code:

3 for August 4 for March

Jan = 1 Feb = 4 Mar = 4

Apr = 0 May = 2 Jun = 5

Jul = 0 Aug = 3 Sep = 6

Oct = 1 Nov = 4 Dec = 6

Step 6: Add your numbers, then mod by 7:

7 + 5 + 1 + 16 + 3 = 32 4 + 2 + 0 + 20 + 4 = 30

32 % 7 = 4 30 % 7 = 2

This is your day of the week, as follows:

Sat = 0 Sun = 1 Mon = 2 Tue = 3 Wed = 4 Thu = 5 Fri = 6

August 16, 1989(Wednesday) March 20, 1950(Monday)

NOTE: some dates require special offsets:

January and February dates in leap years: subtract 1 from step 5

Dates in the 1600s: add 6 to step 5

Dates in the 1700s: add 4 to step 5

Dates in the 1800s: add 2 to step 5

Dates in the 2000s: add 6 to step 5

Dates in the 2100s: add 4 to step 5Example dates: August 16, 1989 and March 20, 1950

Step 1: Only look at the last two digits of the year and determine how many 12s fit in it

7 12s in 89, 4 12s in 50

Step 2: Look at the remainder of this division:

89 – 7 * 12 = 5, 50 – 4 * 12 = 2

Step 3: How many 4s fit into that remainder:

1 four in 5 0 fours in 2

Step 4: Add the day of the month:

16 for the 16th 20 for the 20th

Step 5: Add the month code:

3 for August 4 for March

Jan = 1 Feb = 4 Mar = 4

Apr = 0 May = 2 Jun = 5

Jul = 0 Aug = 3 Sep = 6

Oct = 1 Nov = 4 Dec = 6

Step 6: Add your numbers, then mod by 7:

7 + 5 + 1 + 16 + 3 = 32 4 + 2 + 0 + 20 + 4 = 30

32 % 7 = 4 30 % 7 = 2

This is your day of the week, as follows:

Sat = 0 Sun = 1 Mon = 2 Tue = 3 Wed = 4 Thu = 5 Fri = 6

August 16, 1989(Wednesday) March 20, 1950(Monday)

NOTE: some dates require special offsets:

January and February dates in leap years: subtract 1 from step 5

Dates in the 1600s: add 6 to step 5

Dates in the 1700s: add 4 to step 5

Dates in the 1800s: add 2 to step 5

Dates in the 2000s: add 6 to step 5

Dates in the 2100s: add 4 to step 5

So this is tricky and I'm wondering if someone can help me start this. Seems like it starts off using modulus. Maybe somehow using a code that looks at the last 2 digits of a year but I'm not sure. How should I start this?

posted 5 months ago

The % operator in Java gives you the modulus, or remainder, of a division.

What do you think you'd need to do with it to get the last two digits of the year? Remember that the last two digits are the year within a century, and a century is, ofcourse, 100 years.

What do you think you'd need to do with it to get the last two digits of the year? Remember that the last two digits are the year within a century, and a century is, ofcourse, 100 years.

posted 4 months ago

Start small. On a casual glance, i'd say that each step your teacher broke out should be its own method...possibly each being further broken down into more methods. Step one could be broken down into two methods that "get the last two digits of the year" and "figure out how many 12s fit into it".

The latter could even be written such that you pass in two values - the two digit year part (or really, any number), and a second number as the divisor. So you would then pass in something like 88 and 12, and it would return 7.

The latter could even be written such that you pass in two values - the two digit year part (or really, any number), and a second number as the divisor. So you would then pass in something like 88 and 12, and it would return 7.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

Harry Peters

Ranch Hand

Posts: 39

Campbell Ritchie

Marshal

Posts: 53171

124

posted 4 months ago

You need to say more than “it doesn't work”. We can only help if we know the full details. You need to start small. Start by executing all this code on Eclipse:-Then add one field or one method at a time and execute the code after every change. That way you will find the errors sooner and they will be easier to correct.

Harry Peters

Ranch Hand

Posts: 39

Campbell Ritchie

Marshal

Posts: 53171

124

Harry Peters

Ranch Hand

Posts: 39

posted 4 months ago

Ok been busy and now I've had time to work on this because the whole thing is due Monday. Below is what I have so far and when trying to compile, BlueJ is complaining about line 14. It says, "incompatible types: int cannot be converted to java.lang.String". I need to clean up my coding and make sure I have the whole algorithm correct. As you can see, I have literally followed each step and tried to code it out. If I have started it correctly with the Strings, how can I start the first step like how I did the century part?

Campbell Ritchie

Marshal

Posts: 53171

124

posted 4 months ago

Quite right; you are trying to assign an

Those multiple

`int`to a String. I think the last two digits of the year should be an`int`.Those multiple

`if‑else`s look bad; can you put the numbers into an array? Similarly the days? Or better still put the days into an enumerated type.
Harry Peters

Ranch Hand

Posts: 39

posted 4 months ago

Hi everybody. I realized I put Strings instead of ints which was silly so I updated that. Then BlueJ was giving me the runaround, making me reorder the ints to make sense every time I would try and compile. My algorithm seems circular in logic. Right now the error message says, "variable monthCode might not have been initialized". But when I put the line somewhere else in the method, there is another error. As far as my if statements, that's the only way I can do it. We just started arrays/while loops in the last class a few days ago. My instructor said our big project can be done before last lesson on arrays. I bet that would make everything easier but I'm not sure how to put an array in the middle of this. I already found an array example online for my getMonthName method somewhere else in my class:

Should I put in something like that in my algorithm? And I also must need one for my monthCode if statements. Then maybe I can solve this. Below is my new coding. Any suggestions?

*if(month < 1 || month > 12)*

{

return "null";

}

String[] name = {"January", "February", "March", "April", "May", "June", "July", "August", "September",

"October", "November", "December"};

return name[month-1];{

return "null";

}

String[] name = {"January", "February", "March", "April", "May", "June", "July", "August", "September",

"October", "November", "December"};

return name[month-1];

Should I put in something like that in my algorithm? And I also must need one for my monthCode if statements. Then maybe I can solve this. Below is my new coding. Any suggestions?

Harry Peters

Ranch Hand

Posts: 39

posted 4 months ago

Awesome Knute! That's worked and I can finally compile! So now I'm trying to get the right day returned. When I plug in August 16, 1989, it returns Thursday instead of Wednesday. I don't know how else I can modify my algorithm to get it to work. I posted my update below. Does something stand out?

posted 4 months ago

your calculations for twelves and fours is wrong. Think for a minute, how do you get the number of 5s in 23. If you do 23 % 5, then that will give you 3. Are there only three 5s in 23?

Junilu - [How to Ask Questions] [How to Answer Questions]

posted 4 months ago

Also, I don't see where you made these adjustments:

*NOTE: some dates require special offsets:*

January and February dates in leap years: subtract 1 from step 5

Dates in the 1600s: add 6 to step 5

Dates in the 1700s: add 4 to step 5

Dates in the 1800s: add 2 to step 5

Dates in the 2000s: add 6 to step 5

Dates in the 2100s: add 4 to step 5January and February dates in leap years: subtract 1 from step 5

Dates in the 1600s: add 6 to step 5

Dates in the 1700s: add 4 to step 5

Dates in the 1800s: add 2 to step 5

Dates in the 2000s: add 6 to step 5

Dates in the 2100s: add 4 to step 5

Junilu - [How to Ask Questions] [How to Answer Questions]

Harry Peters

Ranch Hand

Posts: 39

posted 4 months ago

Ok I got it. I made some changes and it seems to work! Now I have one last little piece:

I guess I have to start from the beginning and get the first 2 digits for the centuries. Then just add two things. But then January and February is tricky. My code is below:

*NOTE: some dates require special offsets:*

January and February dates in leap years: subtract 1 from step 5

Dates in the 1600s: add 6 to step 5

Dates in the 1700s: add 4 to step 5

Dates in the 1800s: add 2 to step 5

Dates in the 2000s: add 6 to step 5

Dates in the 2100s: add 4 to step 5

January and February dates in leap years: subtract 1 from step 5

Dates in the 1600s: add 6 to step 5

Dates in the 1700s: add 4 to step 5

Dates in the 1800s: add 2 to step 5

Dates in the 2000s: add 6 to step 5

Dates in the 2100s: add 4 to step 5

I guess I have to start from the beginning and get the first 2 digits for the centuries. Then just add two things. But then January and February is tricky. My code is below:

posted 4 months ago

Leap year: Year is divisible by 4 but not when it is divisible by 100 except when it is also divisible by 400. For example, 1700, 1800, 1900 are NOT leap years because even though they are divisible by 4, they are also divisible by 100. However, even though 1600 and 2000 are divisible by 4 and divisible by 100, they are also divisible by 400, therefore they

*are*leap years. Clear as mud?Junilu - [How to Ask Questions] [How to Answer Questions]

posted 4 months ago

When you see this kind of code:

You should immediately think "Lookup table" - you can use the value of

You should immediately think "Lookup table" - you can use the value of

`mod`as in index into an array (your lookup table). That way, you can do this in one line to do the lookup and another line to declare the lookup table and its set of values. Do you see how the numbers you're comparing mod with can be array indices? So, based on the index, you'd put the appropriate value that you want to return in each element of the lookup array. It's really a quick-and-dirty hashtable.Junilu - [How to Ask Questions] [How to Answer Questions]

Harry Peters

Ranch Hand

Posts: 39

posted 4 months ago

I'm new with arrays but I tried it. After I put in an array(lines 87-90) to return the day of week, BlueJ says I'm missing a return statement. By the way, I put the public final String constant in the variable list you can't see:

Was that what I was supposed to do? And is that what I should do with my other if than statements?

*private static final String[] dayWeek = {"Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"}*;Was that what I was supposed to do? And is that what I should do with my other if than statements?

posted 4 months ago

You took my example too literally. If you only have one index value, you only need one statement, not a whole for-loop to go through all the values. You need to try the example code and understand what it's doing, not just copy it blindly.

Junilu - [How to Ask Questions] [How to Answer Questions]

posted 4 months ago

Part of the problem is your poor choice of variable name. That's hiding the obvious from you. What does the variable mod represent? Why are you calling it "mod"? Is there a better name you can call it to make its purpose more obvious, and therefore make sense to use?

What about this dayWeek array that you declared, the one that you patterned after my example, what are you supposed to do with that? Your method is supposed to figure out what day of the week a given date is, right? So... that array has the strings "Saturday", "Sunday", .... "Friday" in it. The last value you calculate is this "mod" thing whose name, again, doesn't really say much. But it must be good for something, right, otherwise why would you calculate it? Do you see what I'm getting at?

What about this dayWeek array that you declared, the one that you patterned after my example, what are you supposed to do with that? Your method is supposed to figure out what day of the week a given date is, right? So... that array has the strings "Saturday", "Sunday", .... "Friday" in it. The last value you calculate is this "mod" thing whose name, again, doesn't really say much. But it must be good for something, right, otherwise why would you calculate it? Do you see what I'm getting at?

Junilu - [How to Ask Questions] [How to Answer Questions]

Harry Peters

Ranch Hand

Posts: 39

posted 4 months ago

Hi Junilu, I have been working on this and I'm on the last part still:

About that first line: here is what I'm trying:

The error is: "method isLeapYear in class Date cannot be applied to given types; required: int; found: no arguments; reason: actual and formal arguments lists differ in length."

Here is my leap year method:

Can't seem to find the best way to implement this.

NOTE: some dates require special offsets:

January and February dates in leap years: subtract 1 from step 5

Dates in the 1600s: add 6 to step 5

Dates in the 1700s: add 4 to step 5

Dates in the 1800s: add 2 to step 5

Dates in the 2000s: add 6 to step 5

Dates in the 2100s: add 4 to step 5NOTE: some dates require special offsets:

January and February dates in leap years: subtract 1 from step 5

Dates in the 1600s: add 6 to step 5

Dates in the 1700s: add 4 to step 5

Dates in the 1800s: add 2 to step 5

Dates in the 2000s: add 6 to step 5

Dates in the 2100s: add 4 to step 5

About that first line: here is what I'm trying:

The error is: "method isLeapYear in class Date cannot be applied to given types; required: int; found: no arguments; reason: actual and formal arguments lists differ in length."

Here is my leap year method:

Can't seem to find the best way to implement this.

posted 4 months ago

You need to learn how to interpret these error messages. It's not that hard.

"method isLeapYear in class Date" -- there's something with the call you're trying to make to isLeapYear

"cannot be applied to given types" -- you tried to call it with some kind of parameter but the method was expecting some other kind.

"required: int" - the method is expecting to be given an int value for its parameter

"found: no arguments" - you tried to call the method without any arguments

"reason: actual and formal argument lists differ in length" - the number of parameters you called it with (actual) and the number of parameters it was expecting (formal) are different.

The error is: "method isLeapYear in class Date cannot be applied to given types; required: int; found: no arguments; reason: actual and formal arguments lists differ in length."

You need to learn how to interpret these error messages. It's not that hard.

"method isLeapYear in class Date" -- there's something with the call you're trying to make to isLeapYear

"cannot be applied to given types" -- you tried to call it with some kind of parameter but the method was expecting some other kind.

"required: int" - the method is expecting to be given an int value for its parameter

"found: no arguments" - you tried to call the method without any arguments

"reason: actual and formal argument lists differ in length" - the number of parameters you called it with (actual) and the number of parameters it was expecting (formal) are different.

Junilu - [How to Ask Questions] [How to Answer Questions]

Harry Peters

Ranch Hand

Posts: 39

posted 4 months ago

Question: Assuming you have an

HIH

Winston

Harry Peters wrote:Here is my progress:

Trying to work with the centuries...

Question: Assuming you have an

`int`field called '

`year`' that contains a Gregorian year - eg, 1969 or 1753 - what is the value of '

`year / 100`'?

HIH

Winston

"Leadership is nature's way of removing morons from the productive flow" - Dogbert

Articles by Winston can be found here