aspose file tools*
The moose likes Java in General and the fly likes get current day of week problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "get current day of week problem" Watch "get current day of week problem" New topic
Author

get current day of week problem

Marius Constantin
Ranch Hand

Joined: Nov 23, 2011
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

Joined: Sep 20, 2010
Posts: 3647
    
  16

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Aug 05, 2005
Posts: 3548
    
  15
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.


Joanne
Marius Constantin
Ranch Hand

Joined: Nov 23, 2011
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

Joined: Jan 03, 2004
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

Joined: Sep 20, 2010
Posts: 3647
    
  16

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

Joined: Nov 23, 2011
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

Joined: Oct 13, 2005
Posts: 38851
    
  23
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: get current day of week problem