This week's book giveaway is in the Cloud/Virtualizaton forum.We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!

# get current day of week problem

Marius Constantin
Ranch Hand
Posts: 62
Hello Experts !

I am using standard java to get the current day but something is not happening. the output indicates 5 for the day, and today being Tuesday, should indicate 2, right ?

here is my code, please take a look.

Thank you very very much,

Kind regards,
marius

Stephan van Hulst
Bartender
Posts: 5813
61
• 1
It looks like you're assuming that the first day started on Sunday.

Instead of manipulating timestamps yourself, you should use the Calendar class. It does all the hard work for you.

Jeff Verdegan
Bartender
Posts: 6109
6
• 1
Day 0 = Thu. 1/1/1970
Day 1 = Fri. 1/2/1970
...
Day 5 = Tue. 1/6/1070

Also, that's the wrong way to get the current day of the week. Just create a Calendar object for "now" and get its day of the week field.

Joanne Neal
Rancher
Posts: 3742
16
• 1
Your numDays variable will contain the number of days since 1/1/1970, so it all depends on what day that was as to what value you will get by modding it by 7.
have you taken a look at the Calendar class. It has methods that will do this for you.

Marius Constantin
Ranch Hand
Posts: 62
Jeff Verdegan wrote:Day 0 = Thu. 1/1/1970
Day 1 = Fri. 1/2/1970
...
Day 5 = Tue. 1/6/1070

Also, that's the wrong way to get the current day of the week. Just create a Calendar object for "now" and get its day of the week field.

Thank you very much for the answer. Why did you start the count from 0 ?

Jeff Verdegan
Bartender
Posts: 6109
6
Marius Constantin wrote:
Jeff Verdegan wrote:Day 0 = Thu. 1/1/1970
Day 1 = Fri. 1/2/1970
...
Day 5 = Tue. 1/6/1070

Also, that's the wrong way to get the current day of the week. Just create a Calendar object for "now" and get its day of the week field.

Thank you very much for the answer. Why did you start the count from 0 ?

Because that's how modulus works. You're doing day % 7 or whatever, so if you're on the same day of the week as when you start, the result will be 0.

Stephan van Hulst
Bartender
Posts: 5813
61
The "first" element in a sequence (arrays, lists, whatever) is always at index 0. Zero based indexes are also easier to work with when you're using modular arithmetic.

For programmers it's often more natural to start counting at 0.

Marius Constantin
Ranch Hand
Posts: 62
Stephan van Hulst wrote:The "first" element in a sequence (arrays, lists, whatever) is always at index 0. Zero based indexes are also easier to work with when you're using modular arithmetic.

For programmers it's often more natural to start counting at 0.

thank you very much for your explanation !

Campbell Ritchie
Sheriff
Posts: 48981
60
This can be seen in C where an array is equivalent to a pointer.The + 1 bit is here interpreted as moving one place in memory beyond the start of the array.