Tuesday, September 29, 2009

Date Formatter

Is the ParseException happens?

SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy");
Date date = null;
try {
//1
date = df.parse("12-34-2007");
} catch (ParseException e) {
System.out.println("Wrong date");
}
System.out.println(date.toString());

Actually not. To prevent this add to //1
df.setLenient(false);

Letient parameter is true by default that's why to match format strictly need
setting parameter to false

No comments:

Post a Comment