1

I'ma acquiring a date from SQLITE database which is like: 2012-05-07 20:59:45

Trying to execute this code, but it fails

DateFormat formatter = new SimpleDateFormat("dd-MMM-yy");
Date date = formatter.parse("2012-05-07 20:59:45");

How do I do that correctly then? Also tried SimpleDateFormat("dd-MMM-yy HH:mm:ss")

2
  • 2
    The format string used for parsing has to match the expected input May 8, 2012 at 18:04
  • You want the format it is for Parsing. The 1 of 2012 is not the - of dd- is it? May 8, 2012 at 18:15

2 Answers 2

3

Have you tried SimpleDateFormat("yyyy-MM-dd HH:mm:ss")?

2
  • geez, of course. Sorry, my brain is washed out after a day of work :/ May 8, 2012 at 18:17
  • If you're not interested in the time itself, you can convert the Date to a Calendar object, and zero out the hours, minutes and seconds as well (or just use joda-time which has methods to do this sort of thing for you)
    – Matt
    May 8, 2012 at 19:43
0

You try as below,

DateFormat formatter = new SimpleDateFormat("yyy-MM-dd HH:mm:ss");
Date date = formatter.parse("2012-05-07 20:59:45");

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.