JetBrains/intellij-community · error · FormatDecode.IllegalFormatException

precision (''{0}'') not allowed in ''{1}''

Error message

precision (''{0}'') not allowed in ''{1}''

What it means

checkNoPrecision throws when a '.'-based precision is supplied to a conversion that does not accept one. In java.util.Formatter, precision is meaningful only for string, character, and floating-point conversions; date/time conversions ('%tH', '%tY', ...) and others reject it. FormatDecode calls checkNoPrecision while building validators for such conversions, reproducing Formatter's rules inside the inspection.

Source

Thrown at java/java-analysis-impl/src/com/siyeh/ig/format/FormatDecode.java:410

      }
    }

    return parameters.toArray(new Validator[0]);
  }

  private static DateTimeConversionType getDateTimeConversionType(char conversion) {
    return switch (conversion) {
      case 'H', 'I', 'k', 'l', 'M', 'S', 'L', 'N', 'p', 'R', 'T', 'r' -> DateTimeConversionType.TIME;
      case 'z', 'Z' -> DateTimeConversionType.ZONE;
      case 's', 'Q', 'c' -> DateTimeConversionType.ZONED_DATE_TIME;
      case 'B', 'b', 'h', 'A', 'a', 'C', 'Y', 'y', 'j', 'm', 'd', 'e', 'D', 'F' -> DateTimeConversionType.DATE;
      default -> DateTimeConversionType.UNKNOWN;
    };
  }

  private static void checkNoPrecision(String precision, String specifier) {
    if (!StringUtil.isEmpty(precision)) {
      throw new IllegalFormatException(InspectionGadgetsBundle.message("format.string.error.precision.not.allowed", precision, specifier));
    }
  }

  private static boolean isAllBitsSet(int value, int mask) {
    return (value & mask) == mask;
  }

  private static void checkText(String s) {
    if (s.indexOf('%') != -1) {
      throw new IllegalFormatException();
    }
  }

  private static void storeValidator(Validator validator, int pos, ArrayList<Validator> parameters, int argumentCount) {
    if (pos < parameters.size()) {
      final Validator existing = parameters.get(pos);
      if (existing == null) {
        parameters.set(pos, validator);

View on GitHub (pinned to be881553f2)

Solutions

  1. Remove the '.n' precision segment from the offending date/time or precision-less specifier (e.g. "%.3tY" -> "%tY").
  2. If you need truncated date text, format the date fully and substring it afterwards.
  3. For numeric conversions where precision is legal, keep it (e.g. "%.3f"); only the unsupported conversions reject it.

Example fix

// before
String y = String.format("%.3tY", Calendar.getInstance());

// after
String y = String.format("%tY", Calendar.getInstance());
Defensive patterns

Strategy: validation

Validate before calling

static final java.util.Set<Character> PRECISION_OK = java.util.Set.of('s','S','f','e','E','g','G','a','A');
static void checkPrecisionAllowed(String fmt) {
  java.util.regex.Matcher m = java.util.regex.Pattern.compile("%[-#+ 0,(]*\d*(?:\.(\d+))?([a-zA-Z])").matcher(fmt);
  while (m.find()) {
    if (m.group(1) != null && !PRECISION_OK.contains(m.group(2).charAt(0)))
      throw new IllegalArgumentException("precision not allowed for %" + m.group(2));
  }
}

Prevention

When it happens

Trigger: A format string like "%.3tY" or "%.2c"-style misuse — most commonly a precision applied to a date/time conversion (e.g. "%.3TB") — reaching getDateTimeConversionType/checkNoPrecision during decode.

Common situations: Copy-pasting a float specifier and changing only the conversion character to 't'; assuming precision truncates date output; the 'Format String' inspection flagging a calendar-formatting call.

Related errors


AI-assisted analysis of JetBrains/intellij-community@be881553f2 (2026-08-14). Data as JSON: /api/errors/81612627f891ca6d. Report an issue: GitHub.