Before I write the code for this, I want to work it out for a couple other
examples. Probably the easiest example would be January 2nd, 1900 since I know
that would be a Tuesday. I can calculate the days that January 2nd is after
January 1st, by using the daysFrom method on the day object. In this case, it
would give 1. Now, the day of the week would be the reference day plus the
number of days that we are away from it. So, in our case, Monday, plus the
number of days after. And this will work for this case. But if I take a
slightly farther away case, like January 8th, 1900. I know that would be a
Monday again. But it's seven days after. So, I would get that it was 1 plus 7,
which is weekday 8. Weekday 8 doesn't exist. We only have up to weekday six.
So, what I really mean to do is start on Monday, and add seven days. One, two,
three, four, five. Six, seven. So, instead of continuing up to, day 7 and day
8, I go back to day zero and up to day one. So, that means that I'm using mod.
In this case mod 7. So, the uncorrected value in that case would of been 8, and
to correct it, I would calculate that the actual day of the week was the
original one, the uncorrected one, mod 7. The first thing I need to do is
create a day for the reference day and I'm actually not going to use birthday
in this case. So, this is actually the wrong way to do this, because I might
want to use this program to calculate lots of different days. While I'm
testing, I'll make this an easy day, like I just mentioned so, January 8th,
1900 and I'll set up my reference day. So, now I'm going to calculate the days
between the referenceDay and the desiredDay. Using that good old daysFrom
method and now the uncorrectedWeekDay would be the weekday of the referenceDay.
I'll call is referenceWeekDay. Which is a Monday, plus the number of days
between the referenceDay and the day we're interested in. And now to get the
correctedWeekDay, we take mod 7 from the uncorrectedWeekDay. Lets check if I
made any mistakes. I'm pretty sure that my answers should be Monday, that was
what I calculated before. Forgot to say that this is a day. And that one too
and the referenceWeekDay was an int and I can't find a variable weekday.
Because what I called correctedWeekDay should actually just be weekday and that
one should work better. So, now if I run the method, I get that weekday is
minus 6. Must be a bug. So, where could I have gone wrong? Somehow I got a
negative number here. I must have got a negative number because
referenceWeekDay plus daysBetween is a negative number. ReferenceWeekDay is
definitely 1. So, daysBetween must have been negative. It looks like
referenceDay.daysFrom desiredDay is a negative number, but I'm here assuming
that I'm using it to count up. I'm going to check that using the debugger. I'll
put a break point in and I'll run the method. So, when I step over the line
where I initialized daysBetween, I can now see that daysBetween is coming out
as negative seven. So, my suspicion is confirmed. Instead of calling
referenceDay.daysFrom desiredDay, I should have called desiredDay.daysFrom
referenceDay. Let's try this one out. Now I actually get the weekday I
expected. And I'm ready to go back and reset this day to the day I was
interested in, which was June 18th, 1983. Looks like Sally Ride entered low
Earth orbit on a Saturday.