{"record":{"id":"6b7b8f1eaeebfa29","repo":"google/gson","slug":"unknown-dateformat-style-timestyle","errorCode":null,"errorMessage":"Unknown DateFormat style: ${timeStyle}","messagePattern":"Unknown DateFormat style: (.+?)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"gson/src/main/java/com/google/gson/internal/PreJava9DateFormatProvider.java","lineNumber":61,"sourceCode":"        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":43,"sourceCodeEnd":65,"githubUrl":"https://github.com/google/gson/blob/310ac341f2f92a454b229bf21f70d2d18b2b6db7/gson/src/main/java/com/google/gson/internal/PreJava9DateFormatProvider.java#L43-L65","documentation":"PreJava9DateFormatProvider.getTimePartOfDateTimePattern maps a DateFormat style constant to a Java-8-era US time pattern. Recognized values: SHORT, MEDIUM, FULL, LONG (FULL and LONG share the same pattern). Any other value falls through to the default branch and throws IllegalArgumentException (PreJava9DateFormatProvider.java:60).","triggerScenarios":"Calling PreJava9DateFormatProvider.getUsDateTimeFormat with a timeStyle outside {SHORT, MEDIUM, LONG, FULL}, e.g. an unvalidated int from configuration or a mis-typed constant.","commonSituations":"Config-driven date formatting where the time style int comes from a properties file or DB; passing DateFormat.DEFAULT (== MEDIUM coincidentally, but other invalid ints fail); library code reading style ints without validation.","solutions":["Use only the recognized DateFormat constants: SHORT, MEDIUM, LONG, FULL.","Validate the int at the boundary: if (style < 0 || style > 3) throw new IllegalArgumentException(...).","Provide an explicit SimpleDateFormat pattern via GsonBuilder.setDateFormat(\"h:mm:ss a\") to avoid the style mapping entirely."],"exampleFix":"// before\nDateFormat df = PreJava9DateFormatProvider.getUsDateTimeFormat(dateStyle, timeStyle); // timeStyle == 7\n// after\nint timeStyle = (timeStyleRaw < 0 || timeStyleRaw > 3) ? DateFormat.SHORT : timeStyleRaw;\nDateFormat df = PreJava9DateFormatProvider.getUsDateTimeFormat(dateStyle, timeStyle);","handlingStrategy":"validation","validationCode":"if (timeStyle < DateFormat.SHORT || timeStyle > DateFormat.FULL) throw new IllegalArgumentException(\"bad time 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.","Sanitize style ints from external configuration.","Set an explicit SimpleDateFormat pattern via GsonBuilder.setDateFormat(...) to bypass style mapping."],"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"}