google/gson · error · IllegalArgumentException
Unknown DateFormat style: {dateStyle}
Error message
Unknown DateFormat style: {dateStyle} What it means
Thrown by PreJava9DateFormatProvider.getDatePartOfDateTimePattern as IllegalArgumentException when the dateStyle argument is not one of DateFormat.SHORT, MEDIUM, LONG, or FULL. The provider only knows those four standard styles (the ones Java 8 used for US-locale date formatting); any other int (including DateFormat.DEFAULT or custom values) falls through to the default branch.
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 8b8628c656)
Solutions
- Restrict dateStyle to DateFormat.SHORT, MEDIUM, LONG, or FULL; validate before calling.
- If you need a non-standard format, build a SimpleDateFormat with an explicit pattern instead of relying on the style-based factory.
- Map any DEFAULT constant to MEDIUM before forwarding to the provider.
Example fix
// before
DateFormat f = PreJava9DateFormatProvider.getUsDateTimeFormat(99, DateFormat.SHORT); // throws
// after: use a valid constant or explicit pattern
DateFormat f = PreJava9DateFormatProvider.getUsDateTimeFormat(DateFormat.MEDIUM, DateFormat.SHORT);
// or
DateFormat f = new SimpleDateFormat("MMM d, yyyy h:mm a", Locale.US); Defensive patterns
Strategy: validation
Validate before calling
static void checkDateStyle(int style) {
if (style != DateFormat.SHORT && style != DateFormat.MEDIUM
&& style != DateFormat.LONG && style != DateFormat.FULL) {
throw new IllegalArgumentException("Unsupported date style: " + style);
}
} Type guard
static boolean isValidDateStyle(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("dateStyle")) {
dateStyle = DateFormat.DEFAULT == dateStyle ? DateFormat.MEDIUM : DateFormat.MEDIUM;
} else throw e;
} Prevention
- Restrict dateStyle to SHORT/MEDIUM/LONG/FULL.
- Use an explicit SimpleDateFormat pattern for non-standard formats.
- Map DEFAULT to MEDIUM before forwarding.
When it happens
Trigger: Calling getUsDateTimeFormat(dateStyle, timeStyle) with an invalid dateStyle; reached internally by Gson when it builds a default date format for a style constant outside the supported set. Also fires if user code passes DateFormat.DEFAULT (value 2, which collides with MEDIUM only by coincidence) or a negative/arbitrary int.
Common situations: Passing a raw int for date style that is not a recognized DateFormat constant; using a custom style index; version/interop code that forwards an unchecked style value.
Related errors
- Unknown DateFormat style: {timeStyle}
- 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/406135e10f20c811.json.
Report an issue: GitHub.