@Test public void formats() { MatchedDate matchedDate = new MatchedDate("2017-01-16 01:45:05 -0500", null); MatchedDate parse = DateParser.parse(matchedDate, new HttpSource()); assertEquals("2017-01-16T06:45:05", DataUtils.formatInUTC(parse.getDate())); matchedDate = new MatchedDate("Jan 17, 2017 06:08AM ET", null); parse = DateParser.parse(matchedDate, new HttpSource()); assertEquals("2017-01-17T11:08:00", DataUtils.formatInUTC(parse.getDate())); matchedDate = new MatchedDate("January 18, 2017", null); parse = DateParser.parse(matchedDate, new HttpSource()); assertEquals("2017-01-18T00:00:00", DataUtils.formatInUTC(parse.getDate())); assertEquals("2017-01-18T13:45:00", parse("Wed Jan 18 13:45:00 GMT 2017")); assertEquals("2017-02-14T00:00:00", parse("02142017")); assertEquals("2017-02-27T00:00:00", parse("Monday, February 27, 2017 @ 05:02 PM gHale")); assertEquals("2017-03-09T00:00:00", parse("March 9, 2017 8:43 AM")); assertEquals("2017-03-13T15:49:00", parse("13 Mar, 2017 15:49")); assertEquals("2017-03-13T03:14:28", parse("2017-03-13 03:14:28")); assertEquals("2017-03-10T00:00:00", parse("10 March 2017")); assertEquals("2017-03-09T16:47:21", parse("2017-03-09 16:47:21")); assertEquals("2017-03-09T01:00:00", parse("2017-03-09T01:00:00")); assertEquals("2017-03-10T08:24:19", parse("Fri, 10 Mar 2017 13:54:19 +0530")); assertEquals("2016-08-24T06:42:00", parse("Wed Aug 24, 2016 2:42am EDT")); assertEquals("2017-03-10T12:51:00", parse("March 10, 2017, 14:51 IST")); assertEquals("2016-08-22T18:14:26", parse("Mon, 22 Aug 2016 18:14:26 +0000")); assertEquals("2017-04-13T00:00:00", parse("2017/04/13")); }
public static MatchedDate parse(MatchedDate matchedText, HttpSource source) { String value = Strings.nullToEmpty(matchedText.getValue()).trim(); for (String regexp : source.getDateRegexps()) { Pattern pattern = Pattern.compile(regexp); Matcher matcher = pattern.matcher(value); if (matcher.matches() && matcher.groupCount() > 0) { value = matcher.group(1); } } matchedText.setValue(value.replace("ET", "EST")); List<SimpleDateFormat> customFormatters = formatsToFormatters(source.getDateFormats()); MatchedDate md = Stream.concat(customFormatters.stream(), FORMATTERS.stream()) .map(dateFormat -> parse(matchedText, dateFormat)) .filter(matchedDate -> matchedDate.getDate() != null) .findFirst().orElse(matchedText); if (md.getDate() == null && md.getValue() != null) { md = parseWithTimewords(md, source); } return md; }
DateParser { public static MatchedDate parse(MatchedDate matchedText, HttpSource source) { String value = Strings.nullToEmpty(matchedText.getValue()).trim(); for (String regexp : source.getDateRegexps()) { Pattern pattern = Pattern.compile(regexp); Matcher matcher = pattern.matcher(value); if (matcher.matches() && matcher.groupCount() > 0) { value = matcher.group(1); } } matchedText.setValue(value.replace("ET", "EST")); List<SimpleDateFormat> customFormatters = formatsToFormatters(source.getDateFormats()); MatchedDate md = Stream.concat(customFormatters.stream(), FORMATTERS.stream()) .map(dateFormat -> parse(matchedText, dateFormat)) .filter(matchedDate -> matchedDate.getDate() != null) .findFirst().orElse(matchedText); if (md.getDate() == null && md.getValue() != null) { md = parseWithTimewords(md, source); } return md; } }
DateParser { public static MatchedDate parse(MatchedDate matchedText, HttpSource source) { String value = Strings.nullToEmpty(matchedText.getValue()).trim(); for (String regexp : source.getDateRegexps()) { Pattern pattern = Pattern.compile(regexp); Matcher matcher = pattern.matcher(value); if (matcher.matches() && matcher.groupCount() > 0) { value = matcher.group(1); } } matchedText.setValue(value.replace("ET", "EST")); List<SimpleDateFormat> customFormatters = formatsToFormatters(source.getDateFormats()); MatchedDate md = Stream.concat(customFormatters.stream(), FORMATTERS.stream()) .map(dateFormat -> parse(matchedText, dateFormat)) .filter(matchedDate -> matchedDate.getDate() != null) .findFirst().orElse(matchedText); if (md.getDate() == null && md.getValue() != null) { md = parseWithTimewords(md, source); } return md; }  }
DateParser { public static MatchedDate parse(MatchedDate matchedText, HttpSource source) { String value = Strings.nullToEmpty(matchedText.getValue()).trim(); for (String regexp : source.getDateRegexps()) { Pattern pattern = Pattern.compile(regexp); Matcher matcher = pattern.matcher(value); if (matcher.matches() && matcher.groupCount() > 0) { value = matcher.group(1); } } matchedText.setValue(value.replace("ET", "EST")); List<SimpleDateFormat> customFormatters = formatsToFormatters(source.getDateFormats()); MatchedDate md = Stream.concat(customFormatters.stream(), FORMATTERS.stream()) .map(dateFormat -> parse(matchedText, dateFormat)) .filter(matchedDate -> matchedDate.getDate() != null) .findFirst().orElse(matchedText); if (md.getDate() == null && md.getValue() != null) { md = parseWithTimewords(md, source); } return md; }  static MatchedDate parse(MatchedDate matchedText, HttpSource source); static List<MatchedDate> extractFromMeta(Document document); static List<MatchedDate> extractFromProperties(Document document); }
DateParser { public static MatchedDate parse(MatchedDate matchedText, HttpSource source) { String value = Strings.nullToEmpty(matchedText.getValue()).trim(); for (String regexp : source.getDateRegexps()) { Pattern pattern = Pattern.compile(regexp); Matcher matcher = pattern.matcher(value); if (matcher.matches() && matcher.groupCount() > 0) { value = matcher.group(1); } } matchedText.setValue(value.replace("ET", "EST")); List<SimpleDateFormat> customFormatters = formatsToFormatters(source.getDateFormats()); MatchedDate md = Stream.concat(customFormatters.stream(), FORMATTERS.stream()) .map(dateFormat -> parse(matchedText, dateFormat)) .filter(matchedDate -> matchedDate.getDate() != null) .findFirst().orElse(matchedText); if (md.getDate() == null && md.getValue() != null) { md = parseWithTimewords(md, source); } return md; }  static MatchedDate parse(MatchedDate matchedText, HttpSource source); static List<MatchedDate> extractFromMeta(Document document); static List<MatchedDate> extractFromProperties(Document document);  }