3

I get the date string --"2012-04-19 20:51:06". Then I get the milli time by SimpleDateFormat.

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    Date d=sdf.parse("2012-04-19 20:51:06");
    long milliTime=d.getTime()

Then the milliTime is 1334839866000L

However when I convert milliTime to date format String. The result is "2012-04-19 08:51:06"

    long time = 1334839866000L;
    Date date = new Date(time);

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    String t = sdf.format(date);

What's the problem?

0

1 Answer 1

8

Try using HH for the hour component rather than hh.

Sign up to request clarification or add additional context in comments.

3 Comments

+1. The difference being 24-hours format (HH) versus am/pm (hh).
But shouldn't the one format (hh?) throw an exception, when fed with the wrong input (20)?
@user unknown: I haven't done any testing, but the Java Doc says: By default, parsing is lenient: If the input is not in the form used by this object's format method but can still be parsed as a date, then the parse succeeds. Clients may insist on strict adherence to the format by calling setLenient(false).

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.