Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Find Day of The Week Using Any Given Date

Ranch Hand
Posts: 39
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:

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

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?

Java Cowboy
Sheriff
Posts: 16083
88
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.

lowercase baba
Bartender
Posts: 12624
50
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.

Harry Peters
Ranch Hand
Posts: 39
Thanks Fred and Jesper. I found a video of someone accomplishing this using Eclipse but when I try it, it doesn't work. I did it in BlueJ but something is wrong. Can anyone reproduce this and get it to work?

Marshal
Posts: 59101
180
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
Oops, ya as soon as I sent my last post, I realized later I should have been more specific. I'm new at coderanch and I did read the FAQ's, just a little sloppy. I'm going to try working on this method.

Campbell Ritchie
Marshal
Posts: 59101
180
Show us how much you have at the moment which actually works.

Harry Peters
Ranch Hand
Posts: 39
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: 59101
180
Quite right; you are trying to assign an int to a String. I think the last two digits of the year shou‍ld be an int.
Those multiple if‑elses 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
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:

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];

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?

Sheriff
Posts: 4865
136
Try initializing monthCode:

But are the constants JANUARY, FEBRUARY, etc. set up somewhere?

Harry Peters
Ranch Hand
Posts: 39
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?

Sheriff
Posts: 12013
196
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 Lacar
Sheriff
Posts: 12013
196

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

Harry Peters
Ranch Hand
Posts: 39
Ok I got it. I made some changes and it seems to work! Now I have one last little piece:

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

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:

Junilu Lacar
Sheriff
Posts: 12013
196
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 Lacar
Sheriff
Posts: 12013
196
When you see this kind of code:

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 Lacar
Sheriff
Posts: 12013
196
For example:

Harry Peters
Ranch Hand
Posts: 39
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:
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?

Junilu Lacar
Sheriff
Posts: 12013
196
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 Lacar
Sheriff
Posts: 12013
196
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?

Harry Peters
Ranch Hand
Posts: 39
Hi Junilu, I have been working on this and I'm on the last part still:

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

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.

Junilu Lacar
Sheriff
Posts: 12013
196

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.

Harry Peters
Ranch Hand
Posts: 39
Here is my progress:

For January and February dates in leap years.

Trying to work with the centuries. I created an int called centuryCode = 0;

Now when I try and compile, I get an unreachable statement error for line 1.

Bartender
Posts: 10575
66

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

 I have a knack for fixing things like this ... um ... sorry ... here is a consilitory tiny ad: Rocket Oven Kickstarter - from the trailboss https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss