{"id":"4306c125ed02ea73","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/8b8628c65699bc4421696183c62ae0c1b9b281dc/gson/src/main/java/com/google/gson/GsonBuilder.java#L689-L725","documentation":"Thrown by GsonBuilder.checkDateFormatStyle when calling setDateFormat(int, int) with a date or time style integer outside the valid java.text.DateFormat range. Only FULL(0), LONG(1), MEDIUM(2), SHORT(3) are permitted; any other int is rejected at builder time as a programmer error.","triggerScenarios":"Calling gsonBuilder.setDateFormat(dateStyle, timeStyle) where either argument is < 0 or > 3, e.g. setDateFormat(4, 2) or setDateFormat(-1, 0).","commonSituations":"Passing an enum ordinal or config-driven integer that does not map to the four DateFormat constants; copy-pasting a style value from another library (e.g. ICU DateTimeFormatter styles) into Gson; reading a numeric style from a properties file without bounds-checking.","solutions":["Use the named constants DateFormat.FULL/LONG/MEDIUM/SHORT (or 0-3) for both arguments.","If the style comes from external input, validate 0 <= style <= 3 before calling setDateFormat.","Prefer setDateFormat(String pattern) (e.g. \"yyyy-MM-dd\") if you need a custom format and cannot map to a standard style.","Check the full stack trace points to your setDateFormat call and replace the literal."],"exampleFix":"// before\ngsonBuilder.setDateFormat(4, 2);\n// after\nimport java.text.DateFormat;\ngsonBuilder.setDateFormat(DateFormat.LONG, DateFormat.MEDIUM);","handlingStrategy":"validation","validationCode":"// Validate before calling setDateFormat\nprivate static int checkStyle(int style, String name) {\n  if (style < 0 || style > 3) {\n    throw new IllegalArgumentException(\n      name + \" style must be 0..3 (FULL,LONG,MEDIUM,SHORT), got \" + style);\n  }\n  return style;\n}\ngsonBuilder.setDateFormat(checkStyle(ds,\"date\"), checkStyle(ts,\"time\"));","typeGuard":"// Constant guard for known-good styles\nboolean isValidStyle(int s) { return s >= 0 && s <= 3; }","tryCatchPattern":"// Only catch if you must coerce bad config instead of failing fast\ntry {\n  gsonBuilder.setDateFormat(ds, ts);\n} catch (IllegalArgumentException e) {\n  // log and fall back to a known-good style\n  gsonBuilder.setDateFormat(DateFormat.MEDIUM, DateFormat.SHORT);\n}","preventionTips":["Always pass java.text.DateFormat constants, never raw ints.","Unit-test builder construction with the actual config values you ship."],"tags":["gson","date-format","configuration","validation"],"analyzedSha":"8b8628c65699bc4421696183c62ae0c1b9b281dc","analyzedAt":"2026-08-04T19:12:22.202Z","schemaVersion":2}