google/gson · error · IllegalArgumentException

Unknown DateFormat style

Error message

Unknown DateFormat style: ${dateStyle}

What it means

PreJava9DateFormatProvider.getDatePartOfDateTimePattern maps a DateFormat style constant to a Java-8-era US date pattern. Only SHORT, MEDIUM, LONG, FULL are recognized; any other value (including DateFormat.DEFAULT or a raw int) falls through to the default branch and throws IllegalArgumentException (PreJava9DateFormatProvider.java:46).

Solutions

  1. Use only the documented DateFormat style constants: DateFormat.SHORT, MEDIUM, LONG, FULL.
  2. Validate the style int at the boundary: if (style < 0 || style > 3) throw new IllegalArgumentException(...).
  3. Prefer an explicit SimpleDateFormat pattern or a Gson TypeAdapter for Date instead of relying on style constants.

Example fix

// before
DateFormat df = PreJava9DateFormatProvider.getUsDateTimeFormat(dateStyle, timeStyle); // dateStyle == 99
// after
int dateStyle = (dateStyleRaw < 0 || dateStyleRaw > 3) ? DateFormat.MEDIUM : dateStyleRaw;
DateFormat df = PreJava9DateFormatProvider.getUsDateTimeFormat(dateStyle, timeStyle);
Defensive patterns

Strategy: validation

Validate before calling

if (dateStyle < DateFormat.SHORT || dateStyle > DateFormat.FULL) throw new IllegalArgumentException("bad date style");

Type guard

static boolean isKnownStyle(int s) { return s >= 0 && s <= 3; }

Try / catch

try { PreJava9DateFormatProvider.getUsDateTimeFormat(dateStyle, timeStyle); } catch (IllegalArgumentException e) { /* invalid style */ }

Prevention

When it happens

Trigger: Calling PreJava9DateFormatProvider.getUsDateTimeFormat with a dateStyle outside {SHORT, MEDIUM, LONG, FULL}, e.g. passing 2 (DEFAULT) or a negative/arbitrary int. Reachable indirectly when Gson builds default date formats from user-supplied style.

Common situations: Setting GsonBuilder.setDateStyle with an unrecognized constant; passing DateFormat.DEFAULT (=2, which equals MEDIUM but is sometimes confused) or an int read from config without validation; mismatched constant imports.

Related errors


AI-assisted analysis of google/gson@310ac341f2 (2026-08-10). Data as JSON: /api/errors/380581848025bf6e. Report an issue: GitHub.

Appendix: source

Thrown at gson/src/main/java/com/google/gson/internal/PreJava9DateFormatProvider.java:47

   */
  public static DateFormat getUsDateTimeFormat(int dateStyle, int timeStyle) {
    String pattern =
        getDatePartOfDateTimePattern(dateStyle) + " " + getTimePartOfDateTimePattern(timeStyle);
    return new SimpleDateFormat(pattern, Locale.US);
  }

  private static String getDatePartOfDateTimePattern(int dateStyle) {
    switch (dateStyle) {
      case DateFormat.SHORT:
        return "M/d/yy";
      case DateFormat.MEDIUM:
        return "MMM d, yyyy";
      case DateFormat.LONG:
        return "MMMM d, yyyy";
      case DateFormat.FULL:
        return "EEEE, MMMM d, yyyy";
      default:
        throw new IllegalArgumentException("Unknown DateFormat style: " + dateStyle);
    }
  }

  private static String getTimePartOfDateTimePattern(int timeStyle) {
    switch (timeStyle) {
      case DateFormat.SHORT:
        return "h:mm a";
      case DateFormat.MEDIUM:
        return "h:mm:ss a";
      case DateFormat.FULL:
      case DateFormat.LONG:
        return "h:mm:ss a z";
      default:
        throw new IllegalArgumentException("Unknown DateFormat style: " + timeStyle);
    }
  }
}

View on GitHub (pinned to 310ac341f2)