Home > front end >  Parse microseconds datetime format in android kotlin
Parse microseconds datetime format in android kotlin

Time:02-15

I want to parse the following String :

2022-02-14T04:40:33.000000Z

So I'll be able to convert it to this date format:

14/Feb/2022 04:40 or dd/MMM/yyyy HH:mm.

But it throws an exception :

Caused by: java.text.ParseException: Unparseable date: "2022-02-14T04:40:33.000000Z"

My code is a basic SimpleDateFormat with the following code :

val datetime = "2022-02-14T04:40:33.000000Z"

val inputFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.getDefault())
val outputFormat = SimpleDateFormat("dd/MMM/yyyy HH:mm", Locale.getDefault())

val inputDate = inputFormat.parse(date) // exception thrown here.
val dateString = outputFormat.format(inputDate)

I had tried the following format :

  • yyyy-MM-dd'T'HH:mm:ss.SSSZ
  • yyyy-MM-dd'T'HH:mm:ss.SSS'000'Z (I was expecting to ignore the extra 000)

I had read about the PHP DateTime::format which had the u format character for microseconds.

But, the android SimpleDateFormat had no support for the microseconds character.

CodePudding user response:

Please use following format

"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"

  • Related