我有这个时间。晚上8:32:00如何将其转换为24小时Joda-Time LocalTime格式? Joda-Time库中没有时间转换器
答案 0 :(得分:9)
首先将String
解析为LocalTime
...
String timeValue = "8:32:00 PM";
DateTimeFormatter parseFormat = new DateTimeFormatterBuilder().appendPattern("h:mm:ss a").toFormatter();
LocalTime localTime = LocalTime.parse(timeValue, parseFormat);
然后格式化结果......
DateTimeFormatter outputFormat = new DateTimeFormatterBuilder().appendPattern("H:mm:ss").toFormatter();
String formatted = localTime.toString(outputFormat);
System.out.println(formatted);
将输出20:32:00
请记住,LocalTime
的值和格式是两个独立的概念,你不能影响LocalTime
对象本身的“格式”,你只能将值的概念翻译为String
表示(通过格式化程序)
答案 1 :(得分:4)
如果我理解你的问题,
一旦我将时间转换为本地时间,我该如何将其转换为等效的24小时格式?
您可以使用DateTimeFormat
创建DateTimeFormatter
,就像这样 -
DateTimeFormatter fmt = DateTimeFormat.forPattern("HH:mm:ss");
String str = fmt.print(dt);
摘录自链接的javadoc,
H hour of day (0~23) number 0 m minute of hour number 30 s second of minute number 55
答案 2 :(得分:4)
您不希望转换时间(它们是同一时间),您希望格式化时间。 Joda有一个DateTimeFormatter类(详见http://www.joda.org/joda-time/key_format.html)。
您可以使用以下内容:
DateTimeFormatter fmt = DateTimeFormat.forPattern("HH:mm:ss");
String str = date.toString(fmt);