在java中解析时钟时间8 [英] Parse clock time in java 8
本文介绍了在java中解析时钟时间8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如
我想知道是否有可能解析时钟时间: pre> final DateTimeFormatter formatter = DateTimeFormatter.ofPattern(HH:mm:ss);
final String str =12:22:10;
final LocalDateTime dateTime = LocalDateTime.parse(str,formatter);
我试过但是得到这个例外:
<$线程main中的异常java.time.format.DateTimeParseException:Text '12:22:10'无法解析:无法从TemporalAccessor获取LocalDateTime:{},ISO已解析为12:22:10类型为java.time.format.Parsed
解决方案
由于您只有时间使用 LocalTime
类:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern( HH:mm:ss);
String str =12:22:10;
LocalTime time = LocalTime.parse(str,formatter);
请参阅 Oracle教程了解更多信息。
I wonder is it possible to parse clock time hour:minute:second in Java 8?
e.g.
final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
final String str = "12:22:10";
final LocalDateTime dateTime = LocalDateTime.parse(str, formatter);
I tried but get this exception:
Exception in thread "main" java.time.format.DateTimeParseException: Text '12:22:10' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {},ISO resolved to 12:22:10 of type java.time.format.Parsed
解决方案
Since you only have a time use the LocalTime
class:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
String str = "12:22:10";
LocalTime time = LocalTime.parse(str, formatter);
See Oracle Tutorial for more info.
这篇关于在java中解析时钟时间8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文