import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.beanutils.locale.converters.DateLocaleConverter;
class DateLocale {
// 该方法不抛异常
public Date method1(String birthday) throws ParseException {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
return df.parse(birthday);
}
//用到BeanUtils工具包(我用的是commons-beanutils-1.8.3.jar和commons-logging-1.1.1.jar)
// 该方法会报错
public Date method2(String birthday) {
return (Date) new DateLocaleConverter().convert(birthday);
}
}
public class DateLocaleTest {
public static void main(String[] args) {
String birthday = "1990-12-32"; // 一个不存在的日期
Date date = null;
DateLocale dl = new DateLocale();
/*//调用method1
try {
date = dl.method1(birthday);
System.out.println("abcdefg"); //有异常不会执行到这句
} catch (ParseException e) {
System.out.println("有异常!!"); //有异常会输出这句
e.printStackTrace();
}*/
//调用method2
date = dl.method2(birthday);
System.out.println(date.toLocaleString());
}
}
/*
* 调用method1方法输出:
* abcdefg
* 1991-1-1 0:00:00
*/