{"record":{"id":"97f4496c51b13ba9","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/310ac341f2f92a454b229bf21f70d2d18b2b6db7/gson/src/main/java/com/google/gson/internal/bind/DefaultDateTypeAdapter.java#L166-L199","documentation":"DefaultDateTypeAdapter tries every configured DateFormat, then falls back to ISO8601Utils.parse; if all attempts throw ParseException it wraps the final failure in a JsonSyntaxException naming the offending string and JSON path. This is the catch-all 'I could not interpret this token as a Date' error.","triggerScenarios":"Deserializing a JSON string into a java.util.Date (or Date subclass) where the string matches neither the GsonBuilder.setDateFormat pattern(s) nor any ISO8601 variant. Example: format set to 'yyyy-MM-dd' but JSON contains '01/15/2024'.","commonSituations":"Switching GsonBuilder date format after data was produced; locale-sensitive default formats differing between JVMs; consuming an external API whose date format (e.g. epoch millis as number, RFC-1123) Gson doesn't try; mixing SimpleDateFormat instances across threads.","solutions":["Set the matching pattern via GsonBuilder().setDateFormat(\"yyyy-MM-dd'T'HH:mm:ssX\").create().","Register a custom TypeAdapter<Date> that parses the exact incoming format.","If the producer can change, emit ISO8601 (e.g. java.time.Instant toString).","Coerce epoch-millis numbers to Date with a custom deserializer rather than relying on string parsing."],"exampleFix":"// before\nval gson = GsonBuilder().setDateFormat(\"yyyy-MM-dd\").create()\ngson.fromJson(\"\\\"2024-01-15T10:30:00Z\\\"\", Date::class.java) // fails\n\n// after: match the actual input format\nval gson = GsonBuilder().setDateFormat(\"yyyy-MM-dd'T'HH:mm:ssX\").create()\ngson.fromJson(\"\\\"2024-01-15T10:30:00Z\\\"\", Date::class.java)","handlingStrategy":"try-catch","validationCode":"// Pre-parse and validate the date with the configured format\nstatic boolean matchesFormat(String s, String pattern) {\n  try { new SimpleDateFormat(pattern, Locale.US).parse(s); return true; }\n  catch (ParseException e) { return false; }\n}\nGson gson = new GsonBuilder().setDateFormat(\"yyyy-MM-dd'T'HH:mm:ssX\").create();","typeGuard":null,"tryCatchPattern":"try {\n  gson.fromJson(json, DateHolder.class);\n} catch (JsonSyntaxException e) {\n  if (e.getMessage().startsWith(\"Failed parsing \")) {\n    // date format mismatch: adjust setDateFormat or coerce input\n    log.warn(\"Unparseable date: {}\", e.getMessage());\n  }\n  throw e;\n}","preventionTips":["Set an explicit date format on GsonBuilder matching your producer.","Prefer ISO8601 on both ends.","Register a custom Date TypeAdapter for non-standard inputs.","Avoid relying on JVM default locale date formats; they vary."],"tags":["date","parsing","deserialization","date-format","locale"],"backgroundTag":null,"analyzedSha":"310ac341f2f92a454b229bf21f70d2d18b2b6db7","analyzedAt":"2026-08-10T02:58:47.455Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}