Details

CSR

Resolution: Approved

P3

None

behavioral

low

The difference in milliseconds between Instants may be higher than expected by no more than 999 milliseconds. Errors in the difference of microseconds are unlikely, typical Instant values from system clocks have only microsecond precision.

Java API

Implementation
Description
Summary
When computing the time between java.time.Instants
using MILLIS.between(t1, t2)
or MICROS.between(t1, t2)
or t1.until(t2, MILLIS)
or t1.until(t2, MICROS)
the difference may be incorrect.
Problem
The implementation of java.time.Instant.until(Instant t, TemporalUnit unit)
is specialized for computing the number of milliseconds or microseconds between two Instants.
The representation of an Instant is the number of seconds and the number of nanoseconds. The implementation computing the number of units between, as of JDK 18, did not propagate carry and borrow between seconds and nanoseconds when computing milliseconds and microseconds between Instants.
The computations are correct in cases where the difference in nanoseconds is whole milliseconds or microseconds respectively.
Solution
In the cases where the difference in the number of seconds is negative and the difference in the difference of nanoseconds is positive and the case where the difference in seconds is positive and the difference in nanoseconds is negative the difference in the number of seconds is updated.
Specification
There is no change to the specification, the implementation is updated to correctly return the number of milliseconds and microseconds between the instants.
Attachments
Issue Links
 csr of

JDK8307466 java.time.Instant calculation bug in until and between methods
 Closed