当我执行以下代码时,我得到解析异常。任何人都可以帮助我
private Boolean validateDateFormat(HttpSession session, PropertiesHandler props, String startDate,Date sqlDate) {
logger.info("Enter validate");
Boolean isvalid = true;
HashMap hashMap = new LinkedHashMap();
System.out.println("Entered validate block");
if (startDate == null || startDate.equals("")) {
isvalid = false;
hashMap.put("date", props.getText("error.date.compare.cannotbeblank"));
session.setAttribute("errorMessage", hashMap);
System.out.println("Map size " + hashMap.size());
logger.info("Exit validate");
return isvalid;
}
ArrayList<CalendarDatepicker> calList = new ArrayList<CalendarDatepicker>();
String whereClause = " cd.calendar_datepicker="+sqlDate;
calList = (ArrayList<CalendarDatepicker>)dateTimePickerImpl.getCalendarDateDetailsByWhereClause(whereClause);
System.out.append("appointment list size " + calList.size());
if(calList.isEmpty())
{
isvalid=false;
System.out.println("here");
hashMap.put("date", props.getText("error.date.compare.incorrectformat"));
session.setAttribute("errorMessage", hashMap);
System.out.println("Map size " + hashMap.size());
logger.info("Exit validate");
return isvalid;
}
答案 0 :(得分:0)
你在上一条评论中提到:它给出空指针异常,如果我给10/00/2010或10/32/2010 ,则解析异常。
嗯...当然是。它给你一个Unparsable Date例外,因为 10/00/1010 和 10/32/2010 都不是有效日期。
你正在解析日期吗?您应该尝试在try-catch块中包围它:
try {
// parse date here
} catch (ParseException e) {
// handle what do to on parse exception here
}
您可以选择在捕捉不可解决的日期时设置默认日期(例如,月中),或记录错误等。