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

Instant.truncatedTo() rounds up instead of down for instants before Instant.EPOCH

XMLWordPrintable

    • generic
    • generic

      FULL PRODUCT VERSION :
      java version "1.8.0_131"
      Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
      Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)


      ADDITIONAL OS VERSION INFORMATION :
      Linux energy80.workstations.ems 4.10.17-200.fc25.x86_64 #1 SMP Mon May 22 18:12:57 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

      A DESCRIPTION OF THE PROBLEM :
      There is not sub-category for the "java.time" package, so I am forced to submit this bug under a wrong sub-category. Sorry for that.

      java.Instant.truncatedTo(ChronoUnit) seems to produce wrong results if the instant the method is called on is before Instant.EPOCH. I expect this method to always round down, but in this case it rounds up. There is nothing in the JavaDoc that suggests this behaviour.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Execute this main-method:

      public static void main(String[] args) {
      System.out.println(Instant.ofEpochMilli(-1).truncatedTo(ChronoUnit.SECONDS));
      }

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Expected output of the main method:

      1969-12-31T23:59:59Z
      ACTUAL -
      Actual output of the main method:

      1970-01-01T00:00:00Z

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.time.Instant;
      import java.time.temporal.ChronoUnit;

      import org.junit.Assert;

      public class TruncatingInstantTest {
      public static void main(String[] args) {
      Instant expectedResult = Instant.EPOCH.minusSeconds(1);
      Instant truncated = Instant.ofEpochMilli(-1).truncatedTo(ChronoUnit.SECONDS);
      Assert.assertEquals(expectedResult, truncated);
      }
      }

      ---------- END SOURCE ----------

            psonal Pallavi Sonal (Inactive)
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: