在Java 8中解析时钟时间

时间:2016-10-05 09:24:49

标签: parsing datetime java-8 java-time

我想知道是否有可能在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);

我试过但得到了这个例外:

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

2 个答案:

答案 0 :(得分:8)

由于您只有时间使用LocalTime类:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
String str = "12:22:10";
LocalTime time = LocalTime.parse(str, formatter);

有关详细信息,请参阅Oracle Tutorial

答案 1 :(得分:0)

LocalTime.parse("04:17");