* support date patterns like 2015-October-05 or 2015-Oct-05

This commit is contained in:
Reinhard Pointner 2015-10-11 06:19:32 +00:00
parent ae97adb814
commit 55c07b667a
1 changed files with 62 additions and 7 deletions

View File

@ -16,13 +16,19 @@ public class DateMatcher {
private final DatePattern[] patterns;
public DateMatcher() {
patterns = new DatePattern[2];
patterns = new DatePattern[4];
// match yyyy-mm-dd patterns like 2010-10-24, 2009/6/1, etc.
patterns[0] = new DatePattern("(?<!\\p{Alnum})(\\d{4})[^\\p{Alnum}](\\d{1,2})[^\\p{Alnum}](\\d{1,2})(?!\\p{Alnum})", new int[] { 1, 2, 3 });
// match yyyy-mm-dd patterns like 2010-10-24, 2009/6/1, etc
patterns[0] = new NumericDatePattern("(?<!\\p{Alnum})(\\d{4})[^\\p{Alnum}](\\d{1,2})[^\\p{Alnum}](\\d{1,2})(?!\\p{Alnum})", new int[] { 1, 2, 3 });
// match dd-mm-yyyy patterns like 1.1.2010, 01/06/2010, etc.
patterns[1] = new DatePattern("(?<!\\p{Alnum})(\\d{1,2})[^\\p{Alnum}](\\d{1,2})[^\\p{Alnum}](\\d{4})(?!\\p{Alnum})", new int[] { 3, 2, 1 });
// match dd-mm-yyyy patterns like 1.1.2010, 01/06/2010, etc
patterns[1] = new NumericDatePattern("(?<!\\p{Alnum})(\\d{1,2})[^\\p{Alnum}](\\d{1,2})[^\\p{Alnum}](\\d{4})(?!\\p{Alnum})", new int[] { 3, 2, 1 });
// match yyyy.MMMMM.dd patterns like 2015.October.05 etc
patterns[2] = new DateFormatPattern("(?<!\\p{Alnum})(\\d{4})[^\\p{Alnum}](?i:January|February|March|April|May|June|July|August|September|October|November|December)[^\\p{Alnum}](\\d{1,2})(?!\\p{Alnum})", "yyyy MMMMM dd");
// match yyyy.MMM.dd patterns like 2015.Oct.06 etc
patterns[3] = new DateFormatPattern("(?<!\\p{Alnum})(\\d{4})[^\\p{Alnum}](?i:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[^\\p{Alnum}](\\d{1,2})(?!\\p{Alnum})", "yyyy MMM dd");
}
public DateMatcher(DatePattern... patterns) {
@ -74,12 +80,20 @@ public class DateMatcher {
return tail;
}
private static class DatePattern {
private static interface DatePattern {
public SimpleDate match(CharSequence seq);
public int find(CharSequence seq, int fromIndex);
}
private static class NumericDatePattern implements DatePattern {
protected final Pattern pattern;
protected final int[] order;
public DatePattern(String pattern, int[] order) {
public NumericDatePattern(String pattern, int[] order) {
this.pattern = Pattern.compile(pattern);
this.order = order;
}
@ -107,6 +121,47 @@ public class DateMatcher {
return -1;
}
}
private static class DateFormatPattern implements DatePattern {
protected final Pattern space = Pattern.compile("[\\p{Punct}\\p{Space}]+");
protected final Pattern pattern;
protected final String dateFormat;
public DateFormatPattern(String pattern, String dateFormat) {
this.pattern = Pattern.compile(pattern);
this.dateFormat = dateFormat;
}
protected SimpleDate process(MatchResult match) {
return SimpleDate.parse(space.matcher(match.group()).replaceAll(" "), dateFormat);
}
public SimpleDate match(CharSequence seq) {
Matcher matcher = pattern.matcher(seq);
if (matcher.find()) {
return process(matcher);
}
return null;
}
public int find(CharSequence seq, int fromIndex) {
Matcher matcher = pattern.matcher(seq).region(fromIndex, seq.length());
if (matcher.find()) {
if (process(matcher) != null) {
return matcher.start();
}
}
return -1;
}
}
}