I wrote a little piece of code to expose my problem.
public class date {
public static void main(String args[]) {
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy",Locale.ENGLISH);
System.out.println("The date format is : dd-MM-yyyy.");
String date1 = "20-06-2012";
System.out.println("The date1 is : " + date1);
String date2 = "2012-06-20";
System.out.println("The date2 is : " + date2);
try {
System.out.println(formatter.parse(date1).toString());
System.out.println(formatter.parse(date2).toString());
} catch (ParseException e) {
e.printStackTrace();
}
}
}
The output looks like so :
The date format is : dd-MM-yyyy.
The date1 is : 20-06-2012
The date2 is : 2012-06-20
Wed Jun 20 00:00:00 EDT 2012
Mon Dec 03 00:00:00 EST 25
The problem is that I want to have an error raised when the date submitted doesn't match the pattern specified in the SimpleDateFormat, unfortunately, it looks like it sees numbers at the correct position in the string separated by the dashes so it makes it through. Is there another tool to do this or am I wrongly using SimpleDateFormat?