Java 在 Joda-Time 中将 24 小时制时间转换为 am/pm
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19075188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting 24-hour clock time to am/pm in Joda-Time
提问by Sam Haito
I just started working with Joda-Time, and got it to correctly display my date in 24-hour clock("military time") but I would rather it be am/pm. Looked it up and it mentioned hourOfDaywhich I figured was the HH value so I tried to write a loop that would break it down into AM/Pm but it never worked out.
我刚开始使用Joda-Time,并让它以24 小时制(“军用时间”)正确显示我的日期,但我宁愿它是 am/pm。查了一下,它提到了我认为是 HH 值的hourOfDay,所以我尝试编写一个循环,将其分解为 AM/Pm,但它从未奏效。
DateTime dtf = new DateTime(wikiParsedDate);
if (hourOfDay == 00) {
hourOfDay == 12;
DateTimeFormatter builder = DateTimeFormat.forPattern( "dd-MM-yyyy HH:mm:ss.SS'AM" );
return builder.print(dtf);
} else if (0 < hourOfDay && hourOfDay < 12) {
DateTimeFormatter builder = DateTimeFormat.forPattern( "dd-MM-yyyy HH:mm:ss.SS'AM" );
return builder.print(dtf);
} else if (hourOfDay > 12) {
hourOfDay - 12 == hourOfDay;
DateTimeFormatter builder = DateTimeFormat.forPattern( "dd-MM-yyyy HH:mm:ss.SS'PM" );
return builder.print(dtf);
}
}
采纳答案by Jesper
Look at the API documentation of DateTimeFormat
. This should do what you want:
查看DateTimeFormat
. 这应该做你想做的:
DateTimeFormatter builder = DateTimeFormat.forPattern("dd-MM-yyyy hh:mm:ss.SSa");
No need for the complication with different cases.
不需要复杂的不同情况。