Search code examples
javamysqldatesimpledateformat

SimpleDateFormat Validation


I am trying to validate date using Simple date format so that i can put it into MySQL database

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
sdf.setLenient(false);
java.util.Date testDate = sdf.parse("2011-13-05");
System.out.println(testDate);

Gives output, Wed Jan 05 00:13:00 GMT-06:00 2011 Can anyone explain why the date is being converted into time and thus preventing detection of an invalid month 13? Does java.util.date have a date object rather than a datetime object? A modified version of my code will be greatly helpful.


Solution

  • You should use MM for month. mm is for minute in hour.

    new SimpleDateFormat("yyyy-MM-dd");