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

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

XMLWordPrintable

    • b89
    • x86
    • other

        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.

              ntv Nadeesh Tv
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

                Created:
                Updated:
                Resolved: