{"id":"36eee2a46255fd9b","repo":"google/gson","slug":"failed-parsing-s-as-date-at-path-path","errorCode":null,"errorMessage":"Failed parsing '{s}' as Date; at path {path}","messagePattern":"Failed parsing '(.+?)' as Date; at path (.+?)","errorType":"exception","errorClass":"JsonSyntaxException","httpStatus":null,"severity":"error","filePath":"gson/src/main/java/com/google/gson/internal/bind/DefaultDateTypeAdapter.java","lineNumber":184,"sourceCode":"    String s = in.nextString();\n    // Needs to be synchronized since JDK DateFormat classes are not thread-safe\n    synchronized (dateFormats) {\n      for (DateFormat dateFormat : dateFormats) {\n        TimeZone originalTimeZone = dateFormat.getTimeZone();\n        try {\n          return dateFormat.parse(s);\n        } catch (ParseException ignored) {\n          // OK: try the next format\n        } finally {\n          dateFormat.setTimeZone(originalTimeZone);\n        }\n      }\n    }\n\n    try {\n      return ISO8601Utils.parse(s, new ParsePosition(0));\n    } catch (ParseException e) {\n      throw new JsonSyntaxException(\n          \"Failed parsing '\" + s + \"' as Date; at path \" + in.getPreviousPath(), e);\n    }\n  }\n\n  @Override\n  public String toString() {\n    DateFormat defaultFormat = dateFormats.get(0);\n    if (defaultFormat instanceof SimpleDateFormat) {\n      return SIMPLE_NAME + '(' + ((SimpleDateFormat) defaultFormat).toPattern() + ')';\n    } else {\n      return SIMPLE_NAME + '(' + defaultFormat.getClass().getSimpleName() + ')';\n    }\n  }\n}\n","sourceCodeStart":166,"sourceCodeEnd":199,"githubUrl":"https://github.com/google/gson/blob/8b8628c65699bc4421696183c62ae0c1b9b281dc/gson/src/main/java/com/google/gson/internal/bind/DefaultDateTypeAdapter.java#L166-L199","documentation":"DefaultDateTypeAdapter.deserializeToDate throws this JsonSyntaxException after every configured DateFormat AND the ISO8601 fallback parser all failed to parse the string {s}. The {path} is the JsonReader's previous path locating the offending field. This is the terminal failure for date deserialization: Gson has exhausted the date pattern(s) registered on the adapter plus the built-in ISO-8601 attempt.","triggerScenarios":"Deserializing a Date (or Date subclass) where the JSON string does not match any format given to GsonBuilder.setDateFormat(...), the default DEFAULT-style format, the system locale format, the pre-Java-9 US format, or ISO-8601. Example: setDateFormat(\"yyyy-MM-dd\") but input is 'Jan 5, 2025'.","commonSituations":"Backend sends dates in an unexpected format; locale differences (server en_US vs client de_DE); timestamp vs string mismatch; mixing epoch millis with formatted dates; new API version changed the date format.","solutions":["Register a date format matching the producer: new GsonBuilder().setDateFormat(\"MMM d, yyyy\").create().","If multiple formats are possible, register a custom TypeAdapter<Date> that tries each pattern then falls back to ISO-8601 / epoch millis.","If the value is an epoch number, write a small adapter that reads nextLong and constructs new Date(long).","Inspect the {path} and {s} in the exception to identify the exact field and format, then align producer/consumer."],"exampleFix":"// before\nGson gson = new Gson(); // default DEFAULT style\ngson.fromJson(\"{\\\"d\\\":\\\"2025-01-05\\\"}\", Event.class); // JsonSyntaxException\n\n// after\nGson gson = new GsonBuilder().setDateFormat(\"yyyy-MM-dd\").create();\ngson.fromJson(\"{\\\"d\\\":\\\"2025-01-05\\\"}\", Event.class);","handlingStrategy":"try-catch","validationCode":"// Verify a date string parses with your configured format before deserializing\nString s = \"2025-01-05\";\ntry { new SimpleDateFormat(\"yyyy-MM-dd\").parse(s); }\ncatch (ParseException e) { throw new IllegalArgumentException(\"Bad date: \" + s); }","typeGuard":null,"tryCatchPattern":"try {\n  return gson.fromJson(json, Event.class);\n} catch (JsonSyntaxException e) {\n  if (e.getMessage() != null && e.getMessage().startsWith(\"Failed parsing '\")) {\n    throw new InvalidPayloadException(\"Unparseable date in payload; expected format yyyy-MM-dd\", e);\n  }\n  throw e;\n}","preventionTips":["Pin the date format explicitly via GsonBuilder.setDateFormat().","Document the date format in your API contract.","Write a custom TypeAdapter<Date> that tries multiple formats plus epoch-millis."],"tags":["date","deserialization","format","locale"],"analyzedSha":"8b8628c65699bc4421696183c62ae0c1b9b281dc","analyzedAt":"2026-08-04T19:12:22.202Z","schemaVersion":2}