Possible Duplicate:
How to sanity check a date in java
I want to convert a String into Date..with a condition that accepted String format should be only this yyyy/MM/dd-HH:MM:SS
.
Date provided in any other format should give error.
try {
String str_date = "25/09/2012-13:43:20";
DateFormat formatter;
Date date;
formatter = new SimpleDateFormat("yyyy/MM/dd-hh:mm:ss");
date = (Date) formatter.parse(str_date);
System.out.println("Today is " + date);
} catch (ParseException e) {
System.out.println("Exception :" + e);
}
Since str_date
is having format dd/MM/yyyy-hh:mm:ss
, which is invalid it should through exception, but it is not throwing any expection.
The output is Today is Mon Mar 05 13:43:20 IST 31