简体   繁体   English

为什么Joda时间会将输入字符串中的PM更改为AM?

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

My input string is a PM time: 我的输入字符串是PM时间:

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

I'm using Joda Time's pattern syntax as follows to parse the DateTime: 我正在使用Joda Time的模式语法来解析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? 那么,为什么我的输出字符串是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). 您的解析字符串包含“H”,它告诉您的解析器将该值解释为24小时制的一天(0..23)。 So the 6 is interpreted as the 6th hour of the day. 所以6被解释为当天的第6小时。 In the morning. 在早上。 The AM that's printed is because the overall date parsed is considered to be in the morning. 打印的AM是因为解析的整体日期被认为是在早上。

If you want to use 12-hour time, change your format string to: 如果要使用12小时时间,请将格式字符串更改为:

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

'h' will be interpreted as a 12-hour hour of day (1..12) 'h'将被解释为每天12小时(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? H会给你每天0-23的值,所以也许它正在读取它,看到6,并确定它必须是AM?

Try using a lowercase h to get the clockhour of the day. 尝试使用小写h来获得当天的时钟。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Joda DateTimeFormatter.withLocale()只会影响am / pm的字符串值,而不会影响时间格式 - Using Joda DateTimeFormatter.withLocale() only affects the string value for am/pm, not the time format Joda DateTimeFormatter知道上午还是下午 - Joda DateTimeFormatter know if AM or PM 为什么我在Java中使用Joda时间错误地解析此日期时间String? - Why am I parsing this date time String incorrectly using Joda time in Java? 在Joda-Time将24小时制时间转换为上午/下午 - Converting 24-hour clock time to am/pm in Joda-Time Joda Time:无论日期输入如何,为什么Month值默认为Jan? - Joda Time: Why does Month value default to Jan regardless of date input? 我有一个特定的时间作为字符串(“美国东部时间7:00 PM”)。 Joda-Time可以将字符串转换为用户的时区吗? - I have a specific time as a string (“7:00 PM ET”). Can Joda-Time convert that string to the user's time zone? 为什么此输入对于 Joda-Time PeriodFormatter 是无效格式? - Why is this input an invalid format for Joda-Time PeriodFormatter? 如何在 Java 中更改时间格式 - “am/pm”与“AM/PM” - How to change time format in Java - "am/pm" vs. "AM/PM" 为什么Joda-Time-2.3返回不正确的差异? - Why does Joda-Time-2.3 return incorrect difference? 为什么序列化Joda-Time Duration时会出现“ iMillis”? - Why does “iMillis” appear when serializing of Joda-Time Duration?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM