0

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.

1 Answers1

0

Please use following format

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

  • Wow, I didn't expect that the problem laid on the 'Z' instead of the milli/microseconds format. Thanks alot! – Aditya Chandra Feb 15 '22 at 05:52
  • I saw that ignoring 'Z' isn't recommended approach, [here](https://stackoverflow.com/a/58126175/7056462); but for android 26+ we can use `java.time.Instant.parse()` instead. I use the 'Z' format because the format returned always has 'Z' in it, so I need `setTimeZone()` to UTC. – Aditya Chandra Feb 15 '22 at 06:11