1

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

1 Answer 1

3

Try:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I didn't know there were so many different combination in the pattern string. New thing learned! Link for doc: docs.oracle.com/javase/7/docs/api/java/text/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.