| Author |
Help for future date?
|
David Song
Greenhorn
Joined: Oct 15, 2009
Posts: 8
|
|
I want to generate a future uncertain date just based on day just like below:
The result is "Fri Oct 16 15:52:18 CST 2009" , that is tomorrow. But it would be 150 days later, why?
Anyone can help me?
Thanks a lot!
Davids
|
 |
Steve Luke
Bartender
Joined: Jan 28, 2003
Posts: 3041
|
|
The numbers you are doing the math with (150 * 1000 * 60 * 60 * 24) are all integers, which means the result will be an integer. Integers have a max value which this calculation exceeds, so it wraps around and becomes negative - and probably comes back up to being just a few hours ahead instead of a lot of days ahead. Since the result you need has to be a long, you should use longs in the calculation - at least one of the constants should be a long so the rest of them can be widened:
Or make it a little more readable:
Also, take a look at the Calendar class, it has methods built in for doing date math, which you should prefer to use.
|
Steve
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
Steve Luke wrote:Also, take a look at the Calendar class, it has methods built in for doing date math, which you should prefer to use.
Exactly. Calendar will take daylight savings into account. You know, those two days in a year that actually don't have 24 hours.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
David Song
Greenhorn
Joined: Oct 15, 2009
Posts: 8
|
|
|
thank you very much, i got it.
|
 |
 |
|
|
subject: Help for future date?
|
|
|