小编its*_*fix的帖子

将字符串转换为 LocalDate Kotlin

我想转换字符串值,这是我的代码

val dateFirst = "20 Aug 2012"
val dateSecond = "12/16/2020 12:00:00 AM"

val dateFirstConverted = LocalDate.parse(dateFirst, DateTimeFormatter.BASIC_ISO_DATE)
val dateSecondConverted = LocalDate.parse(dateSecond, DateTimeFormatter.BASIC_ISO_DATE)

println(dateFirstConverted)
println(dateSecondConverted)
Run Code Online (Sandbox Code Playgroud)

然后我得到这样的错误。

Exception in thread "main" java.time.format.DateTimeParseException: Text '20 Aug 2012' could not be parsed at index 0
    at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
    at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
    at java.time.LocalDate.parse(LocalDate.java:400)
    at App.TryKt.main(try.kt:11)
    at App.TryKt.main(try.kt)
Run Code Online (Sandbox Code Playgroud)

有人可以帮我解决这个问题吗?

kotlin

6
推荐指数
1
解决办法
2万
查看次数

标签 统计

kotlin ×1