简体   繁体   中英

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

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

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.

My issue solved Thanks @OleV.V.

Solution This pattern: "yyyyMMdd'T'HHmmss"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM