Re: Calculating the difference of days between two dates in Java



Eric Sosman wrote On 07/27/07 15:23,:
[...]

Notes: [...] [3] The while loop may
look slow, but it usually won't execute at all and
will execute more than once only in extraordinary
circumstances (e.g., when time1 and time2 are on
opposite sides of the start of Gregorian reckoning;
maybe not even then).

"Upon further review," as they say in American football,
it's not hard to find a case where the adjustment loop runs
twice. For example, consider time1 at 2007-03-10 23:55:00
and time2 at 2007-03-12 00:05:00. In the USA, the "spring
forward" daylight savings adjustment fell on 2007-03-11, so
that day had only 23 hours. The two times are only 23:10:00
apart, so subtract-and-divide will produce a first estimate
of zero days between time1 and time2. Yet time1 is on the
tenth of the month and time2 on the twelfth; the adjustment
loop will therefore run twice to correct the estimate.

Any pair of times spanning the Standard-to-Daylight
switch can give an off-by-two estimate, if one of them is
very late at night and the other is very early in the morning.
The Daylight-to-Standard "fall back" doesn't seem to lead to
an off-by-two estimate, although it can make off-by-one a
little more likely.


--
Eric.Sosman@xxxxxxx
.



Relevant Pages