Any ideas how can I parse this time with a format ...
# announcements
h
Any ideas how can I parse this time with a format like this: 2017-07-24 034526 +0000 UTC” I tried this but failed with “DateTimeParseException”
import java.time.Instant
import java.time.ZonedDateTime
fun main() {
val time = "2017-07-24 03:45:26 +0000 UTC"
val parsed1: ZonedDateTime = ZonedDateTime.parse(time) // fails
val parsed2: Instant = Instant.parse(time) // fails
}
}
h
This is not ISO8601, so it will fail, because kotlinx DateTime has only an 8601 implementation at the moment
👍 1
d
You need to make your own
DateTimeFormatter
for your format: https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html
h
@hfhbd This doesn't use kotlinx at all; it's not actually Kotlin question but a java.time one. @Hexa
Copy code
import java.time.format.DateTimeFormatter
import java.time.Instant
import java.time.ZonedDateTime

fun main() {
    val time = "2017-07-24 03:45:26 +0000 UTC"
    
    val format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss ZZZ VV")
    val parsed = format.parse(time)
}
👍 3
h
@hho Sorry, you are right. I was confused by the Slack server name, kotlinlang 😄
👍 1