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
- Restrict timeStyle to DateFormat.SHORT, MEDIUM, LONG, or FULL; validate before calling.
- For custom time formats, construct a SimpleDateFormat with an explicit pattern.
- 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
- Restrict timeStyle to SHORT/MEDIUM/LONG/FULL.
- Use an explicit SimpleDateFormat pattern for custom formats.
- Coerce DEFAULT to MEDIUM before invoking the provider.
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
- Unknown DateFormat style: {dateStyle}
- No time zone indicator
- Invalid time zone indicator {timezoneIndicator}
- Failed to parse date [{input}]: {fail.getMessage()}
- {value}
AI-assisted analysis of google/gson@8b8628c656 (2026-08-04).
Data as JSON: /data/errors/fe2c9ac68fb3d416.json.
Report an issue: GitHub.