Search code examples
javadatedate-formatsimpledateformatparseexception

Java SimpleDateFormat Unparseable date


I'm having this awkward problem with simple date format. I'm parsing some strings from a file and want to convert them in Date object. Strings are like

"2012-04-19 18:33:10"

so my code is:

SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD hh:mm:ss");
sdf.setLenient(false);
Date d1 = sdf.parse("2012-04-19 18:33:10");

which gives me

java.text.ParseException: Unparseable date: "2012-04-19 18:33:10"

Without the

setLenient(false)

the output date is

Sun Jan 01 18:33:10 CET 2012

which is very incorrect.

I really don't understand why.

Any help would be appreciated.

Thanks in advance


Solution

  • Try:

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");