google/gson · error · IllegalArgumentException
Invalid style
Error message
Invalid style: ${style} What it means
Thrown by GsonBuilder.checkDateFormatStyle(int) when a date/time style passed to setDateFormat(int) or setDateFormat(int,int) is outside 0..3. Only FULL(0), LONG(1), MEDIUM(2), SHORT(3) are valid DateFormat style constants; anything else is rejected with an IllegalArgumentException at configuration time.
Solutions
- Use one of DateFormat.FULL, DateFormat.LONG, DateFormat.MEDIUM, DateFormat.SHORT.
- If you need a custom format, use setDateFormat(String pattern) instead of the int style overload.
- Validate the constant before calling: ensure it is within 0..3 inclusive.
Example fix
// before builder.setDateFormat(DateFormat.DEFAULT); // -1 -> throws // after builder.setDateFormat(DateFormat.MEDIUM, DateFormat.SHORT);
Defensive patterns
Strategy: validation
Validate before calling
// Validate style constant before calling
static boolean validStyle(int s) { return s >= 0 && s <= 3; }
if (!validStyle(dateStyle) || !validStyle(timeStyle)) {
throw new IllegalArgumentException("style must be one of FULL(0),LONG(1),MEDIUM(2),SHORT(3)");
}
builder.setDateFormat(dateStyle, timeStyle); Try / catch
try { builder.setDateFormat(dateStyle, timeStyle); }
catch (IllegalArgumentException e) {
if (e.getMessage().startsWith("Invalid style")) { /* use a valid constant or pattern */ }
else throw e;
} Prevention
- Use only DateFormat.FULL/LONG/MEDIUM/SHORT.
- Avoid DateFormat.DEFAULT (=-1) here.
- Prefer setDateFormat(String) for custom formats.
When it happens
Trigger: Passing a style value of 4 or higher; passing a negative value; passing an unrelated constant (e.g. a Locale or Calendar constant) by mistake; passing DEFAULT (which is -1 in DateFormat and invalid here).
Common situations: Using DateFormat.DEFAULT (= -1) thinking it is valid; arithmetic on style values that goes out of range; copying a constant from a different enum.
Related errors
- Invalid version
- The date pattern ' ' is not valid
- Class does not implement any supported type adapter class…
- Only combinations of \n and \r are allowed in newline.
- Only combinations of spaces and tabs are allowed in indent.
AI-assisted analysis of google/gson@310ac341f2 (2026-08-10).
Data as JSON: /api/errors/13c6e4d53d6bd978.
Report an issue: GitHub.
Appendix: 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 meansView on GitHub (pinned to 310ac341f2)