This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Help for future date? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help for future date?" Watch "Help for future date?" New topic
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: 4181
    
  21

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: 19697
    
  20

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 - OCEEJBD 6
How To Ask Questions How To Answer Questions
David Song
Greenhorn

Joined: Oct 15, 2009
Posts: 8
thank you very much, i got it.
 
GeeCON Prague 2014
 
subject: Help for future date?