{"record":{"id":"380581848025bf6e","repo":"google/gson","slug":"unknown-dateformat-style-datestyle","errorCode":null,"errorMessage":"Unknown DateFormat style: ${dateStyle}","messagePattern":"Unknown DateFormat style: (.+?)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"gson/src/main/java/com/google/gson/internal/PreJava9DateFormatProvider.java","lineNumber":47,"sourceCode":"   */\n  public static DateFormat getUsDateTimeFormat(int dateStyle, int timeStyle) {\n    String pattern =\n        getDatePartOfDateTimePattern(dateStyle) + \" \" + getTimePartOfDateTimePattern(timeStyle);\n    return new SimpleDateFormat(pattern, Locale.US);\n  }\n\n  private static String getDatePartOfDateTimePattern(int dateStyle) {\n    switch (dateStyle) {\n      case DateFormat.SHORT:\n        return \"M/d/yy\";\n      case DateFormat.MEDIUM:\n        return \"MMM d, yyyy\";\n      case DateFormat.LONG:\n        return \"MMMM d, yyyy\";\n      case DateFormat.FULL:\n        return \"EEEE, MMMM d, yyyy\";\n      default:\n        throw new IllegalArgumentException(\"Unknown DateFormat style: \" + dateStyle);\n    }\n  }\n\n  private static String getTimePartOfDateTimePattern(int timeStyle) {\n    switch (timeStyle) {\n      case DateFormat.SHORT:\n        return \"h:mm a\";\n      case DateFormat.MEDIUM:\n        return \"h:mm:ss a\";\n      case DateFormat.FULL:\n      case DateFormat.LONG:\n        return \"h:mm:ss a z\";\n      default:\n        throw new IllegalArgumentException(\"Unknown DateFormat style: \" + timeStyle);\n    }\n  }\n}\n","sourceCodeStart":29,"sourceCodeEnd":65,"githubUrl":"https://github.com/google/gson/blob/310ac341f2f92a454b229bf21f70d2d18b2b6db7/gson/src/main/java/com/google/gson/internal/PreJava9DateFormatProvider.java#L29-L65","documentation":"PreJava9DateFormatProvider.getDatePartOfDateTimePattern maps a DateFormat style constant to a Java-8-era US date pattern. Only SHORT, MEDIUM, LONG, FULL are recognized; any other value (including DateFormat.DEFAULT or a raw int) falls through to the default branch and throws IllegalArgumentException (PreJava9DateFormatProvider.java:46).","triggerScenarios":"Calling PreJava9DateFormatProvider.getUsDateTimeFormat with a dateStyle outside {SHORT, MEDIUM, LONG, FULL}, e.g. passing 2 (DEFAULT) or a negative/arbitrary int. Reachable indirectly when Gson builds default date formats from user-supplied style.","commonSituations":"Setting GsonBuilder.setDateStyle with an unrecognized constant; passing DateFormat.DEFAULT (=2, which equals MEDIUM but is sometimes confused) or an int read from config without validation; mismatched constant imports.","solutions":["Use only the documented DateFormat style constants: DateFormat.SHORT, MEDIUM, LONG, FULL.","Validate the style int at the boundary: if (style < 0 || style > 3) throw new IllegalArgumentException(...).","Prefer an explicit SimpleDateFormat pattern or a Gson TypeAdapter for Date instead of relying on style constants."],"exampleFix":"// before\nDateFormat df = PreJava9DateFormatProvider.getUsDateTimeFormat(dateStyle, timeStyle); // dateStyle == 99\n// after\nint dateStyle = (dateStyleRaw < 0 || dateStyleRaw > 3) ? DateFormat.MEDIUM : dateStyleRaw;\nDateFormat df = PreJava9DateFormatProvider.getUsDateTimeFormat(dateStyle, timeStyle);","handlingStrategy":"validation","validationCode":"if (dateStyle < DateFormat.SHORT || dateStyle > DateFormat.FULL) throw new IllegalArgumentException(\"bad date style\");","typeGuard":"static boolean isKnownStyle(int s) { return s >= 0 && s <= 3; }","tryCatchPattern":"try { PreJava9DateFormatProvider.getUsDateTimeFormat(dateStyle, timeStyle); } catch (IllegalArgumentException e) { /* invalid style */ }","preventionTips":["Use only the DateFormat.SHORT/MEDIUM/LONG/FULL constants.","Validate style ints read from config before passing them in.","Prefer an explicit SimpleDateFormat pattern to avoid style mapping entirely."],"tags":["gson","date","format","validation"],"backgroundTag":null,"analyzedSha":"310ac341f2f92a454b229bf21f70d2d18b2b6db7","analyzedAt":"2026-08-10T02:58:47.455Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}