日期/时间校验(yyyyMMddHHmmss)

2014-11-23 22:53:52 · 作者: · 浏览: 6

public static boolean checkValidDate(String pDateObj) {

try{

int year = new Integer(pDateObj.substring(0, 4)).intValue();

int month = new Integer(pDateObj.substring(4, 6)).intValue();

int date = new Integer(pDateObj.substring(6, 8)).intValue();

int hourOfDay = new Integer(pDateObj.substring(8, 10)).intValue();

int minute = new Integer(pDateObj.substring(10, 12)).intValue();

int second = new Integer(pDateObj.substring(12, 14)).intValue();

Calendar cal = Calendar.getInstance();

cal.setLenient( false ); //允许严格检查日期格式

cal.set(year, month-1, date);

cal.set(year, month - 1, date, hourOfDay, minute, second);

cal.getTime();//该方法调用就会抛出异常

}catch( Exception e ) {

ret = false;

}

return ret;

}