SimpleDateFormat 24h

2020-05-20 07:29发布

问题:

I've got a SimpleDateFormat to parse a String into a Date:

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ssZ");

When I'm parsing this:

format.parse("2011-08-29T12:44:00+0200");

The result will be, when using Date.toLocaleString:

29 aug. 2011 00:44:00

This should be ofcourse:

29 aug. 2011 12:44:00

And when I'm parsing this:

format.parse("2011-08-29T13:44:00+0200");

Then the result is as expected:

29 aug. 2011 13:44:00

How can I fix this?

回答1:

Use HH instead of hh for the hours pattern:

H   Hour in day (0-23)  Number  0
k   Hour in day (1-24)  Number  24
K   Hour in am/pm (0-11)    Number  0
h   Hour in am/pm (1-12)    Number  12