Làm nó cho mình với một biểu thức chính quy:
public class SpecialDateFormat
{
private final static Pattern PATTERN = Pattern.compile("(\\d{2})[\\.\\/\\-](\\d{2})[\\.\\/\\-](\\d{4}) (\\d{2}):(\\d{2})");
public static Date parse(String text) throws ParseException {
Matcher m = PATTERN.matcher(text);
if (m.matches()) {
int dd = Integer.parseInt(m.group(1));
int mm = Integer.parseInt(m.group(2));
int yy = Integer.parseInt(m.group(3));
int hh = Integer.parseInt(m.group(4));
int mi = Integer.parseInt(m.group(5));
// NOTE: Checking if values are in valid ranges omitted
Calendar cal = Calendar.getInstance();
cal.set(yy, mm - 1, dd, hh, mi, 0);
return cal.getTime();
}
else {
throw new ParseException("Unparseable date: " + text, 0);
}
}
}
Lưu ý tuy nhiên đó điều này cho phép trộn các máy tách khác nhau, ví dụ "17-09/2009 12:00" sẽ được cho phép.
Cảm ơn, tôi sẽ bắt đầu làm việc trên một trình phân tích cú pháp chung cho cơ sở mã của chúng tôi và đây sẽ là một khởi đầu tốt – Tarski