Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

get current day of week problem

 
Marius Constantin
Ranch Hand
Posts: 62
Java Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 6106
72
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Java Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 6106
72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Java Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49776
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic