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

java.time.Instant.truncatedTo(TemporalUnit unit) is truncating up if the year < 1970

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: P3
    • Resolution: Fixed
    • Affects Version/s: 8u51, 9
    • Fix Version/s: 9
    • Component/s: core-libs
    • Subcomponent:
    • Resolved In Build:
      b89
    • CPU:
      x86
    • OS:
      other

      Backports

        Description

        FULL PRODUCT VERSION :
        java version "1.8.0_51"
        Java(TM) SE Runtime Environment (build 1.8.0_51-b16)
        Java HotSpot(TM) 64-Bit Server VM (build 25.51-b03, mixed mode)

        ADDITIONAL OS VERSION INFORMATION :
        Darwin {machine-name} 14.4.0 Darwin Kernel Version 14.4.0: Thu May 28 11:35:04 PDT 2015; root:xnu-2782.30.5~1/RELEASE_X86_64 x86_64

        A DESCRIPTION OF THE PROBLEM :
        According to the JavaDocs (https://docs.oracle.com/javase/8/docs/api/java/time/Instant.html#truncatedTo-java.time.temporal.TemporalUnit-), java.time.Instant's truncatedTo method:

        "Truncating the instant returns a copy of the original with fields smaller than the specified unit set to zero. The fields are calculated on the basis of using a UTC offset as seen in toString. For example, truncating with the MINUTES unit will round down to the nearest minute, setting the seconds and nanoseconds to zero."

        With special emphasis on "truncating with the MINUTES unit will round down to the nearest minute".

        If the year is >1970, this works as described as shown here:

        Instant in1975 = Instant.parse("1975-04-07T03:02:35Z");
        System.out.println("in1975: " + in1975);
        System.out.println("in1975: " + in1975.truncatedTo(ChronoUnit.MINUTES));

        produces:

        in1975: 1975-04-07T03:02:35Z
        in1975: 1975-04-07T03:02:00Z

          
        But if the year is <1970, it actually rounds UP as shown here:

        Instant in1965 = Instant.parse("1965-04-07T03:02:35Z");
        System.out.println("in1965: " + in1965);
        System.out.println("in1965: " + in1965.truncatedTo(ChronoUnit.MINUTES));

        produces:

        in1965: 1965-04-07T03:02:35Z
        in1965: 1965-04-07T03:03:00Z

        For years <1970, according to the JavaDocs it should be:

        in1965: 1965-04-07T03:02:00Z


        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        Instant in1965 = Instant.parse("1965-04-07T03:02:35Z");
        System.out.println("in1965: " + in1965);
        System.out.println("in1965: " + in1965.truncatedTo(ChronoUnit.MINUTES));

        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        in1965: 1965-04-07T03:02:35Z
        in1965: 1965-04-07T03:02:00Z
        ACTUAL -
        in1965: 1965-04-07T03:02:35Z
        in1965: 1965-04-07T03:03:00Z

        REPRODUCIBILITY :
        This bug can be reproduced always.

          Issue Links

            Activity

            Hide
            psonal Pallavi Sonal added a comment - - edited
            Attached Testcase was executed in Windows 7 on
            JDK 8u60 -Fail
            JDK 9eab78 -Fail

            As mentioned by the submitter, the Instant.truncatedTo method truncates to floor in case of dates > 01-01-1970 and truncates to ceiling for dates < 01-01-1970.

            Either the Java Docs should explicitly state that the instant will be rounded down for dates > 01-01-1970 and rounded up for dates less than that , or the output of the method should be uniform irrespective of the year. Moving to dev-team for action.
                  
            Show
            psonal Pallavi Sonal added a comment - - edited Attached Testcase was executed in Windows 7 on JDK 8u60 -Fail JDK 9eab78 -Fail As mentioned by the submitter, the Instant.truncatedTo method truncates to floor in case of dates > 01-01-1970 and truncates to ceiling for dates < 01-01-1970. Either the Java Docs should explicitly state that the instant will be rounded down for dates > 01-01-1970 and rounded up for dates less than that , or the output of the method should be uniform irrespective of the year. Moving to dev-team for action.       
            Hide
            scolebourne Stephen Colebourne added a comment -
            This looks like a bug that should be fixed.
            Show
            scolebourne Stephen Colebourne added a comment - This looks like a bug that should be fixed.
            Hide
            hgupdate HG Updates added a comment -
            URL: http://hg.openjdk.java.net/jdk9/dev/jdk/rev/86713515444c
            User: rriggs
            Date: 2015-10-20 17:26:19 +0000
            Show
            hgupdate HG Updates added a comment - URL: http://hg.openjdk.java.net/jdk9/dev/jdk/rev/86713515444c User: rriggs Date: 2015-10-20 17:26:19 +0000
            Hide
            hgupdate HG Updates added a comment -
            URL: http://hg.openjdk.java.net/jdk9/jdk9/jdk/rev/86713515444c
            User: lana
            Date: 2015-10-28 19:24:44 +0000
            Show
            hgupdate HG Updates added a comment - URL: http://hg.openjdk.java.net/jdk9/jdk9/jdk/rev/86713515444c User: lana Date: 2015-10-28 19:24:44 +0000

              People

              • Assignee:
                ntv Nadeesh Tv (Inactive)
                Reporter:
                webbuggrp Webbug Group
              • Votes:
                0 Vote for this issue
                Watchers:
                4 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: