Home > Net >  How to parse iso8601 date-time in android kotlin or java
How to parse iso8601 date-time in android kotlin or java

Time:06-29

Hello Devs,

I'm working on barcode scanner app, I get date and time at this pattern "20220610T230000Z" I think its ISO8601 date-time format However, I just want to parse this pattern so I can customize it as I want.

I tried this one:

val isoDate="20220610T230000Z" // from my barcode scanner
val df=SimpleDateFormat("yyyymmdd'T'HH:mm:ss.SSS'Z'")
val date= df.parse("20220610T230000Z")

but when i run code i get

java.text.parseexception unparseable

Thanks in advance

CodePudding user response:

isoDate doesn't have any colons or periods in it. Since you're trying to turn isoDate into a Date object, you want something more like:

 val df=SimpleDateFormat("yyyymmdd'T'HHmmssSSS'Z'")

Then, if you want to output a date String with different formatting, you'll have to create a different SimpleDateFormat instance (let's call it dt2) with the intended formatting, and call dt2.format(date). See here for a full example.

CodePudding user response:

My issue solved Thanks @OleV.V.

Solution This pattern: "yyyyMMdd'T'HHmmss"

  • Related