Hello all, is there a way to directly parse a date...
# kotlinx-datetime
c
Hello all, is there a way to directly parse a date string in this format; "MM/dd/yyyy HHmmss" using Kotlin Date time?
LocalDateTime
or
Instant
string parsing methods fail
m
Copy code
fun Instant.toDateString(
    timeZone: TimeZone = TimeZone.currentSystemDefault()
) = toLocalDateTime(timeZone).run {
    "${hour.doubleDigits()}:${minute.doubleDigits()} " +
        "${dayOfMonth.doubleDigits()}.${monthNumber.doubleDigits()}.${year.doubleDigits()}"
}
this one doesn’t fail to me
Ohh you need this too 🙂
Copy code
private const val BIGGEST_DIGIT = 9

fun Int.doubleDigits() = if (this <= BIGGEST_DIGIT) "0$this" else "$this"
c
But you can't convert a date string in that format into a
LocalDateTime
, it'll throw an error