• Post Reply Bookmark Topic Watch Topic
  • New Topic
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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

get current day of week problem

 
Ranch Hand
Posts: 62
Notepad Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Saloon Keeper
Posts: 15490
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Notepad Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Saloon Keeper
Posts: 15490
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Notepad Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 !
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic