Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8307466

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

    XMLWordPrintable

Details

    • b22
    • Verified

    Backports

      Description

        Consider the following code:
         
            var t1 = Instant.parse("2023-05-03T10:00:00.000999Z");
            var t2 = Instant.parse("2023-05-03T10:00:01.000Z");
            var t3 = Instant.parse("2023-05-03T10:00:01.001Z");
            
            System.out.println(MILLIS.between(t1, t2));
            System.out.println(MILLIS.between(t1, t3));
         
        This prints:
         
            1000
            1000
         
        Given that t3 is exactly one millisecond later than t2, it does not seem reasonable that they can both be 1000 milliseconds later than t1.
         
        The JavaDoc for between() states "The calculation returns a whole number, representing the number of complete units between the two temporals."
         
        Assuming I have understood this correctly, I think MILLIS.between(t1, t3) is correct but MILLIS.between(t1, t2) should return 999.

        Reported to core-libs-dev mai:
        https://mail.openjdk.org/pipermail/core-libs-dev/2023-May/105229.html

        Attachments

          Issue Links

            Activity

              People

                rriggs Roger Riggs
                rriggs Roger Riggs
                Votes:
                0 Vote for this issue
                Watchers:
                5 Start watching this issue

                Dates

                  Created:
                  Updated:
                  Resolved: