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

System.out.println("%1$tR", Instant.now()); throws exception

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Won't Fix
    • Icon: P4 P4
    • None
    • 8
    • core-libs
    • x86_64
    • windows_7

      FULL PRODUCT VERSION :
      java version "1.8.0_05"
      Java(TM) SE Runtime Environment (build 1.8.0_05-b13)
      Java HotSpot(TM) 64-Bit Server VM (build 25.5-b02, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.1.7601]

      A DESCRIPTION OF THE PROBLEM :
      According to the JavaDocs of java.util.Formatter, "%1$tR" is a valid conversion which gets the time value of a temporal data type, and it can be used not olny with java.util.Date but also with TemporalAccessor. According to the JavaDocs of java.time.Instant, Instant.now() returns an instance of Instant, which implements Temporal. According to the JavaDocs of java.time.Temporal, it extends TemporalAccessor. Hence, it MUST be valid to use %1$tR on Instance.now().

      Unfortunately it does not. While the pattern works with new Date(), it throws and exception when used with Instance.now().

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Execute the following Java 8 code:

      import java.time.Instant;
      public class TimeFail {
          public static void main(String[] a) {
              System.out.printf("%1$tR", Instant.now()).println();
          }
      }


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Prints the current time (UTC).
      ACTUAL -
      Throws java.util.IllegalFormatConversionException.

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      Exception in thread "main" java.util.IllegalFormatConversionException: H != java.time.Instant
      at java.util.Formatter$FormatSpecifier.print(Unknown Source)
      at java.util.Formatter$FormatSpecifier.print(Unknown Source)
      at java.util.Formatter$FormatSpecifier.print(Unknown Source)
      at java.util.Formatter$FormatSpecifier.printDateTime(Unknown Source)
      at java.util.Formatter$FormatSpecifier.print(Unknown Source)
      at java.util.Formatter.format(Unknown Source)
      at java.io.PrintStream.format(Unknown Source)
      at java.io.PrintStream.printf(Unknown Source)
      at TimeFail.main(TimeFail.java:4)


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.time.Instant;
      public class TimeFail {
          public static void main(String[] a) {
              System.out.printf("%1$tR", Instant.now()).println();
          }
      }

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

      CUSTOMER SUBMITTED WORKAROUND :
      Explicitly convert to UTC zoned timestamp:

      import java.time.Instant;
      import java.time.ZoneId;
      public class TimeFail {
          public static void main(String[] a) {
              System.out.printf("%1$tR", Instant.now().atZone(ZoneId.of("Z"))).println();
          }
      }


            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: