我很新的Java
/Android
的发展。 我想写一个简单的Android应用程序,并且作为它的一部分,我需要将日期从字符串转换为日期。SimpleDateFormat :: parse()被跳过
我有以下方法:
private Date convertFromString(String birthdate) {
String regex = "^(?:(?:31(\\/|-|\\.)(?:0?[13578]|1[02]))\\1|(?:(?:29|30)(\\/|-|\\.)(?:0?[1,3-9]|1[0-2])\\2))(?:(?:1[6-9]|[2-9]\\d)?\\d{2})$|^(?:29(\\/|-|\\.)0?2\\3(?:(?:(?:1[6-9]|[2-9]\\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\\d|2[0-8])(\\/|-|\\.)(?:(?:0?[1-9])|(?:1[0-2]))\\4(?:(?:1[6-9]|[2-9]\\d)?\\d{2})$\n";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(birthdate);
Date date = null;
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy", Locale.UK);
if (matcher.matches()) {
try {
Calendar cal = Calendar.getInstance(); // <-- this,
cal.setTime(format.parse(birthdate)); // and that line gets skipped by debugger step
System.out.print(cal); // this line gets executed
} catch (ParseException exception) {
System.out.print("wtf??");
}
}
return date;
}
无论在传递给方法的字符串值,它总是返回null
。当我用上面标记的调试器线执行此代码时,只能通过调试器跳过,并且它不会让我介入,好像format.parse(..)
从来没有被调用过?
有留在方法的一些调试代码故意
方法调用过程中没有异常被抛出,我通过有效的数据!
抛出异常?重建项目有帮助吗? –
因为它会抛出一个异常... – Selvin
重建多次,没有异常抛出,如果是那么容易,我不会问这个.. –