google/gson · error · IllegalArgumentException

Invalid style: {style}

Error message

Invalid style: {style}

What it means

Thrown by GsonBuilder.checkDateFormatStyle when calling setDateFormat(int, int) with a date or time style integer outside the valid java.text.DateFormat range. Only FULL(0), LONG(1), MEDIUM(2), SHORT(3) are permitted; any other int is rejected at builder time as a programmer error.

Source

Thrown at gson/src/main/java/com/google/gson/GsonBuilder.java:707

   * @param dateStyle the predefined date style that date objects will be serialized/deserialized
   *     to/from
   * @param timeStyle the predefined style for the time portion of the date objects
   * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
   * @throws IllegalArgumentException if the style values are invalid
   * @since 1.2
   */
  @CanIgnoreReturnValue
  public GsonBuilder setDateFormat(int dateStyle, int timeStyle) {
    this.dateStyle = checkDateFormatStyle(dateStyle);
    this.timeStyle = checkDateFormatStyle(timeStyle);
    this.datePattern = null;
    return this;
  }

  private static int checkDateFormatStyle(int style) {
    // Valid DateFormat styles are: 0, 1, 2, 3 (FULL, LONG, MEDIUM, SHORT)
    if (style < 0 || style > 3) {
      throw new IllegalArgumentException("Invalid style: " + style);
    }
    return style;
  }

  /**
   * Configures Gson for custom serialization or deserialization. This method combines the
   * registration of an {@link TypeAdapter}, {@link InstanceCreator}, {@link JsonSerializer}, and a
   * {@link JsonDeserializer}. It is best used when a single object {@code typeAdapter} implements
   * all the required interfaces for custom serialization with Gson. If a type adapter was
   * previously registered for the specified {@code type}, it is overwritten.
   *
   * <p>This registers the type specified and no other types: you must manually register related
   * types! For example, applications registering {@code boolean.class} should also register {@code
   * Boolean.class}. And when registering an adapter for a class which has subclasses, you might
   * also want to register the adapter for subclasses, or use {@link
   * #registerTypeHierarchyAdapter(Class, Object)} instead.
   *
   * <p>{@link JsonSerializer} and {@link JsonDeserializer} are made "{@code null}-safe". This means

View on GitHub (pinned to 8b8628c656)

Solutions

  1. Use the named constants DateFormat.FULL/LONG/MEDIUM/SHORT (or 0-3) for both arguments.
  2. If the style comes from external input, validate 0 <= style <= 3 before calling setDateFormat.
  3. Prefer setDateFormat(String pattern) (e.g. "yyyy-MM-dd") if you need a custom format and cannot map to a standard style.
  4. Check the full stack trace points to your setDateFormat call and replace the literal.

Example fix

// before
gsonBuilder.setDateFormat(4, 2);
// after
import java.text.DateFormat;
gsonBuilder.setDateFormat(DateFormat.LONG, DateFormat.MEDIUM);
Defensive patterns

Strategy: validation

Validate before calling

// Validate before calling setDateFormat
private static int checkStyle(int style, String name) {
  if (style < 0 || style > 3) {
    throw new IllegalArgumentException(
      name + " style must be 0..3 (FULL,LONG,MEDIUM,SHORT), got " + style);
  }
  return style;
}
gsonBuilder.setDateFormat(checkStyle(ds,"date"), checkStyle(ts,"time"));

Type guard

// Constant guard for known-good styles
boolean isValidStyle(int s) { return s >= 0 && s <= 3; }

Try / catch

// Only catch if you must coerce bad config instead of failing fast
try {
  gsonBuilder.setDateFormat(ds, ts);
} catch (IllegalArgumentException e) {
  // log and fall back to a known-good style
  gsonBuilder.setDateFormat(DateFormat.MEDIUM, DateFormat.SHORT);
}

Prevention

When it happens

Trigger: Calling gsonBuilder.setDateFormat(dateStyle, timeStyle) where either argument is < 0 or > 3, e.g. setDateFormat(4, 2) or setDateFormat(-1, 0).

Common situations: Passing an enum ordinal or config-driven integer that does not map to the four DateFormat constants; copy-pasting a style value from another library (e.g. ICU DateTimeFormatter styles) into Gson; reading a numeric style from a properties file without bounds-checking.

Related errors


AI-assisted analysis of google/gson@8b8628c656 (2026-08-04). Data as JSON: /data/errors/4306c125ed02ea73.json. Report an issue: GitHub.