{"record":{"id":"fcc8036496f7e18f","repo":"stanfordnlp/CoreNLP","slug":"could-not-parse-date-string-refdatestr","errorCode":null,"errorMessage":"Could not parse date string: [${refDateStr}]","messagePattern":"Could not parse date string: \\[(.+?)\\]","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/time/TimeExpressionExtractorImpl.java","lineNumber":182,"sourceCode":"          continue;\n        }\n        assert timex != null;  // Timex.fromMap never returns null and if it exceptions, we've already done a continue\n        cm.set(TimeAnnotations.TimexAnnotation.class, timex);\n        coreMaps.add(cm);\n      }\n    }\n    return coreMaps;\n  }\n\n  public List<TimeExpression> extractTimeExpressions(CoreMap annotation, String refDateStr, SUTime.TimeIndex timeIndex) {\n    SUTime.Time refDate = null;\n    if (refDateStr != null) {\n      try {\n        // TODO: have more robust parsing of document date?  docDate may not have century....\n        // TODO: if docDate didn't change, we can cache the parsing of the docDate and not repeat it for every sentence\n        refDate = SUTime.parseDateTime(refDateStr,true);\n      } catch (Exception e) {\n        throw new RuntimeException(\"Could not parse date string: [\" + refDateStr + \"]\", e);\n      }\n    }\n    return extractTimeExpressions(annotation, refDate, timeIndex);\n  }\n\n  public List<TimeExpression> extractTimeExpressions(CoreMap annotation, SUTime.Time refDate, SUTime.TimeIndex timeIndex) {\n    if (!annotation.containsKey(CoreAnnotations.NumerizedTokensAnnotation.class)) {\n      try {\n        List<CoreMap> mergedNumbers = NumberNormalizer.findAndMergeNumbers(annotation);\n        annotation.set(CoreAnnotations.NumerizedTokensAnnotation.class, mergedNumbers);\n      } catch (NumberFormatException e) {\n        logger.warn(\"Caught bad number: \" + e.getMessage());\n        annotation.set(CoreAnnotations.NumerizedTokensAnnotation.class, new ArrayList<>());\n      }\n    }\n\n    List<? extends MatchedExpression> matchedExpressions = expressionExtractor.extractExpressions(annotation);\n    List<TimeExpression> timeExpressions = new ArrayList<>(matchedExpressions.size());","sourceCodeStart":164,"sourceCodeEnd":200,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/time/TimeExpressionExtractorImpl.java#L164-L200","documentation":"Thrown by extractTimeExpressions when the reference date string cannot be parsed by SUTime.parseDateTime. The refDate anchors all relative time expressions in the annotated text; a bad string aborts extraction. Same root cause as the docDate variant, but on the String-based overload used by timeExpressions().","triggerScenarios":"Calling extractTimeExpressions(annotation, refDateStr, timeIndex) (or timeExpressions(annotation, refDateStr)) with refDateStr like \"today\", \"2020\", \"Tue Sep 8 00:12:33 PDT 2020\" — anything Joda-time ISO parsing with lenient=false cannot handle.","commonSituations":"Feeding Java Date.toString() output, locale-formatted dates, or partial dates from Tika/document metadata into the SUTime pipeline.","solutions":["Convert the reference date to ISO 8601 (yyyy-MM-ddTHH:mm:ss) before passing it.","Catch the RuntimeException and retry with null refDate so the extractor skips date anchoring.","Use the SUTime.ISO_DATE_FORMAT-style pattern (or Joda ISODateTimeFormat) to format your Date object instead of toString().","Pre-validate with DateTimeFormat.forPattern(...).parseDateTime(refDateStr) in a try-catch and fall back."],"exampleFix":"// before\nString ref = new Date().toString(); // \"Tue Sep 08 ... PDT 2020\"\nextractor.extractTimeExpressions(annotation, ref, timeIndex);\n\n// after\nString ref = ISODateTimeFormat.dateTime().print(new DateTime()); // \"2026-09-09T12:00:00.000\"\nextractor.extractTimeExpressions(annotation, ref, timeIndex);","handlingStrategy":"validation","validationCode":"static boolean isValidRefDate(String s) {\n  if (s == null) return true;\n  try { ISODateTimeFormat.dateTimeParser().parseDateTime(s); return true; }\n  catch (IllegalArgumentException e) { return false; }\n}","typeGuard":null,"tryCatchPattern":"try {\n  return extractor.extractTimeExpressions(annotation, refDateStr, timeIndex);\n} catch (RuntimeException e) {\n  if (e.getMessage() != null && e.getMessage().startsWith(\"Could not parse date string\")) {\n    return extractor.extractTimeExpressions(annotation, (String) null, timeIndex);\n  } else throw e;\n}","preventionTips":["Format reference dates with ISODateTimeFormat, never Date.toString().","Unit-test rule pipelines with representative refDate strings from your data source.","Centralize refDate construction in one utility that guarantees ISO output."],"tags":["date-parsing","runtime-exception","sutime","nlp"],"backgroundTag":"invalid-date-format","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}