1
Timestamp tsmp = Timestamp.valueOf("0302-02-20 00:00:00");
final DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
System.out.println(df.format(tsmp));

this code segment prints : 0302-02-20 12:00:00 why is that? Can anyone please explain?

3 Answers 3

8

You're using hh, which is 12-hour format ("Hour in am/pm (1-12)" as specified in the docs). Use HH instead.

final DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

You should pretty much never use hh unless you're also using a, the AM/PM designator.

(Did you really mean to create a timestamp in the year 302AD though, out of interest?)

3

new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

Read the API again. The time symbols are not all lower case characters.

1
  • 1
    The OP already realized they weren't all lower case, as he was using MM correctly.
    – Jon Skeet
    Jun 12, 2013 at 4:42
2

DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

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.