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:

# how to create an algorithm that returns the String dayOfTheWeek in a given date

Greenhorn
Posts: 22
Hello,

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 util instead I have to create the algorithm by myself. Needless to say that I don't even know where to start. Is there anyone out there that could point me out to the right direction?
Thanks a lot.

Bartender
Posts: 1844
10
Not allowed to use java.util.Calendar....
Well I guess I can understand they want you to understand what underlies that library.

To work out what day of the week it is, you need a reference point. A point to start from.

For instance if you know that 01 Jan 2015 was a Thursday, you can work out what day of the week any other day in our calendar is by figuring out how many days different there are between that day and 01 Jan 2015.
So 03 Jan 2015 would be a Saturday because it is two days later.

Sound doable?

Stephen Corrad
Greenhorn
Posts: 22
mm..I know what to do step by step, but unfortunately I don't know how to traslate it into java code. For Example:

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
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 March 20, 1950
Wednesday Monday

Stefan Evans
Bartender
Posts: 1844
10
Ah. ok.
That does make a difference. You have an algorithm already. Its just not in computer language!
It might just need a bit more fleshing out.

So start at the beginning.
How are you going to get the date into the program?
Does the user type it in?
All in one String? ie "Month Date, FourDigitYear" ie MM dd, yyyy
Or do you ask for Month, Day, Year separately?

My advice would be to break down the steps further:
- "Only look at the last two digits of the year" - work out how to get that into a variable - probably an int.

- determine how many 12s fit in it. Once you have the two digit year in an int, you can divide it by 12. Define a variable for that value and continue
...

Stephen Corrad
Greenhorn
Posts: 22
are you suggesting to create local variables inside the getDayOfTheWeek() method for each value I have to calculate?

public class Date{

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

public String getDayOfTheWeek(){

int lastTwoDigits = year % 100;
int lastTwoDigitsDividedByTwelve = lastTwoDigits / 12;
int lastTwoDigitsRemainder = lastTwoDigits - (lastTwoDigitsDividedByTwelve * 12);

and so on???

Stefan Evans
Bartender
Posts: 1844
10
Looks good to me.

You can also get the lastTwoDigitsRemainder using the modulo operator

I'm a little torn on it, but you might want to define for instance

int step1 = lastTwoDigitsDividedByTwelve;
int step2 = ...

So that it translates into your algorithm more directly.
I hate badly named variables, but in this case, it might help with mapping which number is which.

Ranch Hand
Posts: 789
Looking ok, but just fyi what you want is called a Perpetual Calendar.
http://en.wikipedia.org/wiki/Perpetual_calendar#Algorithms

Also this, which I didn't know about before.
http://en.wikipedia.org/wiki/Zeller%27s_congruence

Stephen Corrad
Greenhorn
Posts: 22
this is what I have so far...

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

return (lastTwoDigits + lastTwoDigitsDivTwelve + lastTwoDigitsRemainder + lastTwoDigitsRemainderDivFour + day + ..???)

}

the last value that I have to add is the month code (please see the list above). How can I add them to my algorithm. Also, do you think this is going to work?

Thanks in advance

Stefan Evans
Bartender
Posts: 1844
10
So you have to associate your variable "month" which is an int with one of the values you have defined for jan, feb, march etc

Couple of ways to do that:
Assuming that January is month 1

if statements

Or a switch statement

switch (month) {
case 1 : monthAdjustment = January; break;
case 2 : monthAdjustment = February; break;
}

You have effectively defined constants for all of those values, which makes it a bit verbose.
This would be a bit shorter, but a little harder to understand:

Another way to do it
Create an array indexed by the number of the month.

Edit: And Yes. Once you fix up your missing semicolons and other little syntax error problems, you should be good.
One thing I would suggest - print out your working values along the way.
Compare it to the workings with the example date. Do your individual step elements agree with the example given?

Stephen Corrad
Greenhorn
Posts: 22

unfortunately my code doesn't work. I have the following error: incompatible types: int cannot be converted to java.lang.string

Stefan Evans
Bartender
Posts: 1844
10

You're missing one final step - turning that number into a String.
I would suggest just doing a

That will get rid of that compile error. And you can fix it to return a proper value later.

Stephen Corrad
Greenhorn
Posts: 22
ok so I guess the code is working now! The problem is still the same. How do I assign to the month the value of the month's table??

 It is sorta covered in the JavaRanch Style Guide.