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

IllegalFormatConversionException message is misleading for %t argument mismatch

    XMLWordPrintable

    Details

    • Subcomponent:
    • CPU:
      generic
    • OS:
      generic

      Description

      ADDITIONAL SYSTEM INFORMATION :
      $ uname -a
      Linux aquarius 4.19.0-17-amd64 #1 SMP Debian 4.19.194-2 (2021-06-21) x86_64 GNU/Linux
      $ java -version
      openjdk version "17" 2021-09-14
      OpenJDK Runtime Environment Temurin-17+35 (build 17+35)
      OpenJDK 64-Bit Server VM Temurin-17+35 (build 17+35, mixed mode, sharing)


      A DESCRIPTION OF THE PROBLEM :
      When a java.util.Formatter format string contains %t, but the corresponding argument is not a valid temporal type, the resulting IllegalFormatConversionException has a misleading message. The message does not mention the 't' conversion character at all; instead, only the character following the 't' is in the message.

      For example, this:

      new Formatter().format("%tc", "dummy");

      ...produces this exception:

      java.util.IllegalFormatConversionException: c != java.lang.String

      Of course, the conversion mismatch has nothing to do with the use of 'c'. The message should read:

      java.util.IllegalFormatConversionException: t != java.lang.String


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Pass an argument which is neither Long, Date, Calendar, nor TemporalAccessor, along with a format string containing "%t", to a Formatter.format method.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The resulting exception should indicate that the argument is not suitable for a %t conversion.
      ACTUAL -
      The resulting exception implies that a different conversion character was the problem.

      ---------- BEGIN SOURCE ----------
      import java.util.Formatter;

      public class FormatterInvalidArgTest {
          public static void main(String[] args) {
              new Formatter().format("%tc", "dummy");
          }
      }

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

      CUSTOMER SUBMITTED WORKAROUND :
      Become accustomed to recognizing that "c != <type>" may refer to %tc (and extend that to all date-time conversions).

      FREQUENCY : always


        Attachments

          Activity

            People

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

              Dates

              Created:
              Updated: