Joda-Time String to LocalTime 24小时等效

use*_*206 3 java time date jodatime

我有这个时间.晚上8:32:00 如何将其转换为24小时Joda-Time LocalTime格式?Joda-Time库中没有时间转换器

Mad*_*mer 10

首先解析String为LocalTime...

String timeValue = "8:32:00 PM";
DateTimeFormatter parseFormat = new DateTimeFormatterBuilder().appendPattern("h:mm:ss a").toFormatter();
LocalTime localTime = LocalTime.parse(timeValue, parseFormat);
Run Code Online (Sandbox Code Playgroud)

然后格式化结果......

DateTimeFormatter outputFormat = new DateTimeFormatterBuilder().appendPattern("H:mm:ss").toFormatter();
String formatted = localTime.toString(outputFormat);
System.out.println(formatted);
Run Code Online (Sandbox Code Playgroud)

哪个会输出 20:32:00

请记住,值LocalTime和格式是两个独立的概念,你不能影响LocalTime对象本身的"格式" ,你只能将值的概念转换为String表示(通过格式化程序)