{"record":{"id":"13c6e4d53d6bd978","repo":"google/gson","slug":"invalid-style-style","errorCode":null,"errorMessage":"Invalid style: ${style}","messagePattern":"Invalid style: (.+?)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"gson/src/main/java/com/google/gson/GsonBuilder.java","lineNumber":707,"sourceCode":"   * @param dateStyle the predefined date style that date objects will be serialized/deserialized\n   *     to/from\n   * @param timeStyle the predefined style for the time portion of the date objects\n   * @return a reference to this {@code GsonBuilder} object to fulfill the \"Builder\" pattern\n   * @throws IllegalArgumentException if the style values are invalid\n   * @since 1.2\n   */\n  @CanIgnoreReturnValue\n  public GsonBuilder setDateFormat(int dateStyle, int timeStyle) {\n    this.dateStyle = checkDateFormatStyle(dateStyle);\n    this.timeStyle = checkDateFormatStyle(timeStyle);\n    this.datePattern = null;\n    return this;\n  }\n\n  private static int checkDateFormatStyle(int style) {\n    // Valid DateFormat styles are: 0, 1, 2, 3 (FULL, LONG, MEDIUM, SHORT)\n    if (style < 0 || style > 3) {\n      throw new IllegalArgumentException(\"Invalid style: \" + style);\n    }\n    return style;\n  }\n\n  /**\n   * Configures Gson for custom serialization or deserialization. This method combines the\n   * registration of an {@link TypeAdapter}, {@link InstanceCreator}, {@link JsonSerializer}, and a\n   * {@link JsonDeserializer}. It is best used when a single object {@code typeAdapter} implements\n   * all the required interfaces for custom serialization with Gson. If a type adapter was\n   * previously registered for the specified {@code type}, it is overwritten.\n   *\n   * <p>This registers the type specified and no other types: you must manually register related\n   * types! For example, applications registering {@code boolean.class} should also register {@code\n   * Boolean.class}. And when registering an adapter for a class which has subclasses, you might\n   * also want to register the adapter for subclasses, or use {@link\n   * #registerTypeHierarchyAdapter(Class, Object)} instead.\n   *\n   * <p>{@link JsonSerializer} and {@link JsonDeserializer} are made \"{@code null}-safe\". This means","sourceCodeStart":689,"sourceCodeEnd":725,"githubUrl":"https://github.com/google/gson/blob/310ac341f2f92a454b229bf21f70d2d18b2b6db7/gson/src/main/java/com/google/gson/GsonBuilder.java#L689-L725","documentation":"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.","triggerScenarios":"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).","commonSituations":"Using DateFormat.DEFAULT (= -1) thinking it is valid; arithmetic on style values that goes out of range; copying a constant from a different enum.","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."],"exampleFix":"// before\nbuilder.setDateFormat(DateFormat.DEFAULT); // -1 -> throws\n\n// after\nbuilder.setDateFormat(DateFormat.MEDIUM, DateFormat.SHORT);","handlingStrategy":"validation","validationCode":"// Validate style constant before calling\nstatic boolean validStyle(int s) { return s >= 0 && s <= 3; }\nif (!validStyle(dateStyle) || !validStyle(timeStyle)) {\n  throw new IllegalArgumentException(\"style must be one of FULL(0),LONG(1),MEDIUM(2),SHORT(3)\");\n}\nbuilder.setDateFormat(dateStyle, timeStyle);","typeGuard":null,"tryCatchPattern":"try { builder.setDateFormat(dateStyle, timeStyle); }\ncatch (IllegalArgumentException e) {\n  if (e.getMessage().startsWith(\"Invalid style\")) { /* use a valid constant or pattern */ }\n  else throw e;\n}","preventionTips":["Use only DateFormat.FULL/LONG/MEDIUM/SHORT.","Avoid DateFormat.DEFAULT (=-1) here.","Prefer setDateFormat(String) for custom formats."],"tags":["gson-builder","date-format","configuration","validation"],"backgroundTag":null,"analyzedSha":"310ac341f2f92a454b229bf21f70d2d18b2b6db7","analyzedAt":"2026-08-10T02:58:47.455Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}