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

半城伤御伤魂 提交于 2019-12-08 16:29:42

问题


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 output string AM?

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

回答1:


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)




回答2:


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.



来源:https://stackoverflow.com/questions/4683950/why-does-joda-time-change-the-pm-in-my-input-string-to-am

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!