google/gson · error · IllegalArgumentException

Unknown DateFormat style: {timeStyle}

Error message

Unknown DateFormat style: {timeStyle}

What it means

Thrown by PreJava9DateFormatProvider.getTimePartOfDateTimePattern as IllegalArgumentException when the timeStyle argument is not one of DateFormat.SHORT, MEDIUM, LONG, or FULL. Note LONG and FULL map to the same time pattern ('h:mm:ss a z'); any other int (including DEFAULT or out-of-range values) hits the default branch and is rejected.

Source

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

        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 8b8628c656)

Solutions

  1. Restrict timeStyle to DateFormat.SHORT, MEDIUM, LONG, or FULL; validate before calling.
  2. For custom time formats, construct a SimpleDateFormat with an explicit pattern.
  3. Coerce DEFAULT to MEDIUM before invoking the provider.

Example fix

// before
DateFormat f = PreJava9DateFormatProvider.getUsDateTimeFormat(DateFormat.SHORT, 7); // throws

// after: valid constant or explicit pattern
DateFormat f = PreJava9DateFormatProvider.getUsDateTimeFormat(DateFormat.SHORT, DateFormat.MEDIUM);
// or
DateFormat f = new SimpleDateFormat("M/d/yy h:mm:ss a", Locale.US);
Defensive patterns

Strategy: validation

Validate before calling

static void checkTimeStyle(int style) {
  if (style != DateFormat.SHORT && style != DateFormat.MEDIUM
      && style != DateFormat.LONG && style != DateFormat.FULL) {
    throw new IllegalArgumentException("Unsupported time style: " + style);
  }
}

Type guard

static boolean isValidTimeStyle(int s) {
  return s == DateFormat.SHORT || s == DateFormat.MEDIUM
      || s == DateFormat.LONG || s == DateFormat.FULL;
}

Try / catch

try {
  PreJava9DateFormatProvider.getUsDateTimeFormat(dateStyle, timeStyle);
} catch (IllegalArgumentException e) {
  if (e.getMessage().contains("timeStyle")) {
    timeStyle = DateFormat.MEDIUM;
  } else throw e;
}

Prevention

When it happens

Trigger: Calling getUsDateTimeFormat(dateStyle, timeStyle) with an invalid timeStyle; or internal Gson usage when constructing a default time format whose style constant is outside the four supported values.

Common situations: Passing DateFormat.DEFAULT or a raw int for time style; forwarding an unchecked style from configuration/interop code; assuming all DateFormat.* constants are supported.

Related errors


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