Eduardo Rafael Moraes
Eduardo Rafael Moraes

Reputation: 373

convert string date to milliseconds in android

I'm having trouble converting the string format date = 2021-05-10T13: 30: 09Z to Date in the android kotlin I tried with the code below but at the time of the conversion ParseException is returned

val a = "2021-05-10T13:30:09Z"
    try {
        val sdf = SimpleDateFormat("dd/MM/yyyy hh:mm:ss")
        val date: Date = sdf.parse(a)
    } catch (e: ParseException){
        e.printStackTrace()
    }

Upvotes: 0

Views: 323

Answers (1)

msimic
msimic

Reputation: 76

I think this question is the same.

Your string format is a ISO 8601 format and a nice solution would be something like this:

val ta = DateTimeFormatter.ISO_INSTANT.parse(a)
val i = Instant.from(ta)
val d = Date.from(i)

Upvotes: 4

Related Questions