• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help for future date?

 
David Song
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20494
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David Song
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you very much, i got it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic