
Signature
Tony Morris
http://tmorris.net/
>> How do I get the date exactly 21 days from now?
>final long now = System.currentTimeMillis();
>final long then = now + 1000 * 60 * 60 * 24 * 21;
If this time span contains a day that includes a change of
daylight saving time mode, that day might have more or less
than 24 hours. For certain values of »now« (around midnight)
the second »then« might be part of a day, whose date might not
be considered to be »21 days from now«. Far more rare, a
similar effect could be triggered by leap seconds.
P.Hill - 21 May 2006 20:43 GMT
>>>How do I get the date exactly 21 days from now?
>>
[quoted text clipped - 6 lines]
> the second »then« might be part of a day, whose date might not
> be considered to be »21 days from now«.
> Far more rare, a
> similar effect could be triggered by leap seconds.
Stephan's warning about 23 and 25 hour days related to Daylight Savings
is worth noting. The concern with leap seconds is much less likely.
That is unless the hardware on your computer includes the leap second,
your VM understands that, the libraries work with it, and the binary
value you are working with was stored and converted with those rules,
but otherwise you won't have to worry about this particular problem.
A general rule for calendar calculations is to convert all items to the
target units first then combine them. In this case (if you weren't
using the Calendar classes), you'd convert Now to some linear number of
days (Julian Days is a good one) then add 21 and convert back to
whatever form you want.
-Paul