Android parse String to Date - unknown pattern character 'X'

前端 未结 13 1058
耶瑟儿~
耶瑟儿~ 2021-01-31 07:32

I have Service fetch date string from web and then I want to pare it to Date object. But somehow application crashes. This is my string that I\'m parsi

13条回答
  •  暖寄归人
    2021-01-31 08:12

    according to android documentation zone offset with X format is supporting in API level 24+

    Letter  Date or Time Component      Supported (API Levels)
    X       Time zone                   24+
    

    so we can't use for lower APIs, I found a workaround for this issue:

    SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").format(date).let {
        StringBuilder(it).insert(it.length - 2, ":").toString()
    }
    

提交回复
热议问题