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

Instant.now() incorrectly represents nano seconds

XMLWordPrintable

      ADDITIONAL SYSTEM INFORMATION :
      openjdk version "11.0.11" 2021-04-20
      OpenJDK Runtime Environment 18.9 (build 11.0.11+9)
      OpenJDK 64-Bit Server VM 18.9 (build 11.0.11+9, mixed mode)


      OS Name: Microsoft Windows 10 Enterprise
      OS Version: 10.0.19042 N/A Build 19042

      System Model: HP EliteBook 850 G7 Notebook PC
      System Type: x64-based PC
      Processor(s): 1 Processor(s) Installed.
                                 [01]: Intel64 Family 6 Model 142 Stepping 12 GenuineIntel ~1803 Mhz
      BIOS Version: HP S73 Ver. 01.03.02, 11/4/2020

      A DESCRIPTION OF THE PROBLEM :
      If we use "Instant.now().toString()" it is differently represented:
      "2021-05-04T12:52:25.707903600Z" vs "2021-05-04T12:52:25.708903Z"
      Ending "00 suffix " looks s

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      It was checked in OpenJDK 11 and OpenJDK16.
      Just print Instant.now().toString() in the circle of >=1000 items

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      All records should look the same with nanos or without them, but not with ending zeros:
      "2021-05-04T12:52:25.707903600Z" vs "2021-05-04T12:52:25.708903Z"
      ACTUAL -
      ...
      2021-05-04T12:52:25.723904200Z
      2021-05-04T12:52:25.724904Z
      ...
      2021-05-04T12:52:25.724904Z
      2021-05-04T12:52:25.725904900Z
      ...
      209 from 1000 are truncated

      ---------- BEGIN SOURCE ----------
      public class Main {

          public static void main(String[] args) {
              var truncatedAmount = 0;
              var testedAmount = 1_000;
              for (var i = 0; i < testedAmount; i++) {
                  var instantAsString = Instant.now().toString();
                  System.out.println(instantAsString);
                  if (instantAsString.length() <= 27) {
                      truncatedAmount++;
                  }
              }
              System.out.printf("%s from %s are truncated%n", truncatedAmount, testedAmount);
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      In our case this issue causes the incorrect bahaviour for com.fasterxml.jackson.datatype.jsr310.ser.InstantSerializer.
      Custom TruncatedInstantSerializer was introduced by using Instant.truncatedTo(ChronoUnit.MICROS) to make the results consistent.

      FREQUENCY : always


            naoto Naoto Sato
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: