Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse clock time in java 8

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
like image 886
Kaizar Laxmidhar Avatar asked Oct 05 '16 09:10

Kaizar Laxmidhar


1 Answers

LocalTime.parse

Since you only have a time use the LocalTime class. No need to define a formatting pattern in your case.

String str = "12:22:10";
LocalTime time = LocalTime.parse(str);

See that code run live at IdeOne.com.

See Oracle Tutorial for more info.

like image 149
greg-449 Avatar answered Sep 29 '22 18:09

greg-449