开发者

Why does Joda time change the PM in my input string to AM?

My input string is a PM time:

    log(start);
    // Sunday, January 09, 2011 6:30:00 PM

I'm using Joda Time's pattern syntax as follows to parse the DateTime:

    DateTimeFormatter parser1 = 
    DateTimeFormat.forPattern("EEEE, MMMM dd, yyyy H:mm:ss aa");
    DateTime startTime = parser1.parseDateTime(start);

So, why is my 开发者_如何学JAVAoutput string AM?

    log(parser1.print(startTime));
    // Sunday, January 09, 2011 6:30:00 AM


Your parse string contains "H" which is telling your parser to interpret the value as a 24-hour hour of day (0..23). So the 6 is interpreted as the 6th hour of the day. In the morning. The AM that's printed is because the overall date parsed is considered to be in the morning.

If you want to use 12-hour time, change your format string to:

"EEEE, MMMM dd, yyyy h:mm:ss aa".

'h' will be interpreted as a 12-hour hour of day (1..12)


H will give you a 0-23 value of each day, so maybe it's reading off of that, seeing the 6, and determining that it must be AM?

Try using a lowercase h to get the clockhour of the day.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜