将String转换为Joda LocalTime格式(HH:mm:ss)并删除毫秒
问题描述:
DateTimeFormatter fmt = DateTimeFormat.forPattern("HH:mm:ss");
LocalTime localTime = fmt.parseLocalTime("02:51:20");
System.out.println("LocalTime: "+localTime);
我只需要02:51:20,但输出为02:51:20.000。有什么方法可以从输出中删除.000(毫秒)。
I need only 02:51:20 but it outputs as 02:51:20.000. Is there any way that I can remove the .000 (milliseconds) from the output.
答
是的 - 你只需要格式化当你打印出来。目前,您只是使用默认的 toString()
表示。如果您对已经使用的格式化程序感到满意,可以使用:
Yes - you just need to format when you print out. Currently you're just using the default toString()
representation. If you're happy with the formatter you've already got, you can just use:
System.out.println("LocalTime: " + fmt.print(localTime));