D
D
devpy142021-06-28 07:28:33
Kotlin
devpy14, 2021-06-28 07:28:33

Why does an error occur when converting String to LocalTime?

The following code throws an exception: Exception in thread "main" java.time.format.DateTimeParseException: Text '8:20:35' could not be parsed at index 0

val time = "8:20:35"
val localTime = LocalTime.parse(time)


I tried to pass the formatter there - it does not help:
val time = "8:20:35"
val formatter = DateTimeFormatter.ofPattern("HH:mm:ss", Locale.getDefault())
val localTime = LocalTime.parse(time, formatter)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sand, 2021-06-29
@devpy14

Because your string does not comply with ISO-8601, it should not be "8", but "08"
val time = "08:20:35"
Here is the documentation

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question