java 将字符串转换为 Joda LocalTime 格式 (HH:mm:ss) 并删除毫秒
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27989498/
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
Convert String to Joda LocalTime format (HH:mm:ss) and Remove milliseconds
提问by souashokraj
DateTimeFormatter fmt = DateTimeFormat.forPattern("HH:mm:ss");
LocalTime localTime = fmt.parseLocalTime("02:51:20");
System.out.println("LocalTime: "+localTime);
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.
我只需要 02:51:20 但它输出为 02:51:20.000。有什么方法可以从输出中删除 .000(毫秒)。
回答by Jon Skeet
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:
是的 - 您只需要在打印时进行格式化。目前您只是使用默认toString()
表示。如果您对已有的格式化程序感到满意,则可以使用:
System.out.println("LocalTime: " + fmt.print(localTime));
回答by Semih Eker
You can easily use your DateTimeFormetter print() method
. Try like this;
您可以轻松地使用您的DateTimeFormetter print() method
. 像这样尝试;
DateTimeFormatter fmt = DateTimeFormat.forPattern("HH:mm:ss");
LocalTime localTime = fmt.parseLocalTime("02:51:20");
System.out.println("LocalTime : " + localTime);
System.out.println("LocalTime with DateTimeFormatter : " + fmt.print(localTime));
And the output is;
输出是;
LocalTime : 02:51:20.000
LocalTime with DateTimeFormatter : 02:51:20
回答by Jayani Sumudini
Fixed this using
修复了这个使用
DateTime time = new DateTime();
DateTimeFormatter format = DateTimeFormat.forPattern("HH:mm:ss");
String localTime = time != null ? format.print(new LocalTime(time.getHourOfDay(),
time.getMinuteOfHour(), time.getSecondOfMinute())) : null;
DateTime and LocalTime are from joda
DateTime 和 LocalTime 来自 joda