您可以像下面这样为不同的String格式构造SimpleDateFormat对象(
null如果无法将参数解析为有效日期则返回):
// Initializing possibleFormats somewhere only onceSimpleDateFormat[] possibleFormats = new SimpleDateFormat[] { new SimpleDateFormat("yyyy-MM-dd"), new SimpleDateFormat("yyyy,MM,dd"), new SimpleDateFormat("yyyy,MM,dd,HH,mm") };for (SimpleDateFormat format: possibleFormats){ format.setLenient(false);}// initializing endspublic Date parseDate(String date) { Date retVal = null; int index = 0; while (retVal == null && index < possibleFormats.length) { try { retVal = possibleFormats[index++].parse(date); } catch (ParseException ex) { } } return retVal;}


