{"id":"406135e10f20c811","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/8b8628c65699bc4421696183c62ae0c1b9b281dc/gson/src/main/java/com/google/gson/internal/PreJava9DateFormatProvider.java#L29-L65","documentation":"Thrown by PreJava9DateFormatProvider.getDatePartOfDateTimePattern as IllegalArgumentException when the dateStyle argument is not one of DateFormat.SHORT, MEDIUM, LONG, or FULL. The provider only knows those four standard styles (the ones Java 8 used for US-locale date formatting); any other int (including DateFormat.DEFAULT or custom values) falls through to the default branch.","triggerScenarios":"Calling getUsDateTimeFormat(dateStyle, timeStyle) with an invalid dateStyle; reached internally by Gson when it builds a default date format for a style constant outside the supported set. Also fires if user code passes DateFormat.DEFAULT (value 2, which collides with MEDIUM only by coincidence) or a negative/arbitrary int.","commonSituations":"Passing a raw int for date style that is not a recognized DateFormat constant; using a custom style index; version/interop code that forwards an unchecked style value.","solutions":["Restrict dateStyle to DateFormat.SHORT, MEDIUM, LONG, or FULL; validate before calling.","If you need a non-standard format, build a SimpleDateFormat with an explicit pattern instead of relying on the style-based factory.","Map any DEFAULT constant to MEDIUM before forwarding to the provider."],"exampleFix":"// before\nDateFormat f = PreJava9DateFormatProvider.getUsDateTimeFormat(99, DateFormat.SHORT); // throws\n\n// after: use a valid constant or explicit pattern\nDateFormat f = PreJava9DateFormatProvider.getUsDateTimeFormat(DateFormat.MEDIUM, DateFormat.SHORT);\n// or\nDateFormat f = new SimpleDateFormat(\"MMM d, yyyy h:mm a\", Locale.US);","handlingStrategy":"validation","validationCode":"static void checkDateStyle(int style) {\n  if (style != DateFormat.SHORT && style != DateFormat.MEDIUM\n      && style != DateFormat.LONG && style != DateFormat.FULL) {\n    throw new IllegalArgumentException(\"Unsupported date style: \" + style);\n  }\n}","typeGuard":"static boolean isValidDateStyle(int s) {\n  return s == DateFormat.SHORT || s == DateFormat.MEDIUM\n      || s == DateFormat.LONG || s == DateFormat.FULL;\n}","tryCatchPattern":"try {\n  PreJava9DateFormatProvider.getUsDateTimeFormat(dateStyle, timeStyle);\n} catch (IllegalArgumentException e) {\n  if (e.getMessage().contains(\"dateStyle\")) {\n    dateStyle = DateFormat.DEFAULT == dateStyle ? DateFormat.MEDIUM : DateFormat.MEDIUM;\n  } else throw e;\n}","preventionTips":["Restrict dateStyle to SHORT/MEDIUM/LONG/FULL.","Use an explicit SimpleDateFormat pattern for non-standard formats.","Map DEFAULT to MEDIUM before forwarding."],"tags":["gson","date","dateformat","illegalargumentexception","pre-java9"],"analyzedSha":"8b8628c65699bc4421696183c62ae0c1b9b281dc","analyzedAt":"2026-08-04T19:12:22.202Z","schemaVersion":2}