For a reason, yes. Here is a working example that accepts various formats.
I assume the German / European format as follows:
DD. MM. YYYY HH:MM:SS:MMMM
(this means that I cannot match any date format where the month is coming)
Here's the class:
public class VariableDateParser {
private static final Pattern DATE_PATTERN = Pattern
.compile("((?:(?:\\d+(?:[./]\\s*)?)+)?)\\s*((?:(?:\\d+[:]?)+)?)");
public Date getDate(final String dateString) {
final Calendar calendar = Calendar.getInstance();
final Matcher matcher = DATE_PATTERN.matcher(dateString);
if (matcher.matches()) {
final String dateGroup = matcher.group(1).trim();
if (!"".equals(dateGroup)) {
final Iterator<Integer> fields = Arrays.asList(
Calendar.DATE, Calendar.MONTH, Calendar.YEAR).iterator();
final String[] items = dateGroup.split("\\D+");
for (final String item : items) {
if ("".equals(item))
break;
else if (fields.hasNext()) {
final Integer field = fields.next();
calendar.set(field, Integer.parseInt(item) -
(field.equals(Calendar.MONTH) ? 1 : 0));
} else {
throw new IllegalArgumentException(
"Bad date part: " + dateGroup);
}
}
}
final String timeGroup = matcher.group(2).trim();
if (!"".equals(timeGroup)) {
final Iterator<Integer> fields = Arrays.asList(
Calendar.HOUR, Calendar.MINUTE, Calendar.SECOND,
Calendar.MILLISECOND).iterator();
final String[] items = timeGroup.split("\\D+");
for (final String item : items) {
if ("".equals(item))
break;
else if (fields.hasNext()) {
final Integer field = fields.next();
calendar.set(field, Integer.parseInt(item));
} else {
throw new IllegalArgumentException(
"Bad time part: " + timeGroup);
}
}
}
} else
throw new IllegalArgumentException(
"Bad date string: " + dateString);
return calendar.getTime();
}
}
Test code:
public static void main(final String[] args) {
VariableDateParser parser = new VariableDateParser();
DateFormat df = DateFormat.getDateTimeInstance(
DateFormat.MEDIUM, DateFormat.LONG, Locale.GERMAN);
System.out.println(df.format(parser.getDate("11")));
System.out.println(df.format(parser.getDate("11. 10.")));
System.out.println(df.format(parser.getDate("11. 10. 4")));
System.out.println(df.format(parser.getDate("11. 10. 2004")));
System.out.println(df.format(parser.getDate("11. 10. 2004 11")));
System.out.println(df.format(parser.getDate("11. 10. 2004 11:35")));
System.out.println(df.format(parser.getDate("11. 10. 2004 11:35:18")));
System.out.println(df.format(parser.getDate("11. 10. 2004 11:35:18:123")));
System.out.println(df.format(parser.getDate("11:35")));
System.out.println(df.format(parser.getDate("11:35:18")));
System.out.println(df.format(parser.getDate("11:35:18:123")));
}
Conclusion:
11.05.2011 15:57:24 MESZ
11.10.2011 15:57:24 MESZ
11.10.0004 15:57:24 MEZ
11.10.2004 15:57:24 MESZ
11.10.2004 23:57:24 MESZ
11.10.2004 23:35:24 MESZ
11.10.2004 23:35:18 MESZ
11.10.2004 23:35:18 MESZ
01.05.2011 13:35:24 MESZ
01.05.2011 13:35:18 MESZ
01.05.2011 13:35:18 MESZ
Note:
This is a quick proof of concept, not a serious attempt to write such a class. This will conform to many invalid formats and ignore many acceptable ones.
source
share