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

前端 未结 13 1009
耶瑟儿~
耶瑟儿~ 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 07:49

    Remove "XXX" from

    DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
    

    and everything would work fine.

    Go through the list of symbols that can be used inside a SimpleDateFormat constructor. Although the documentation shows the "XXX" format, this doesn't work on Android and will throw an IllegalArgumentException.

    Probably you are looking for "yyyy-MM-dd'T'HH:mm:ss.SSSZ"

    Change your code to

    DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS"); 
    

    or

    DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"); // if timezone is required
    

提交回复
热议问题