SimpleDateFormat “Unparseable date” Exception

后端 未结 8 1788
再見小時候
再見小時候 2020-12-20 16:55

I am trying to parse datetime string with SimpleDateFormat.parse() but I keep receiving Unparseable date exceptions.

Here is the date format I am trying

8条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-20 17:51

    I am sure most of you got frustrated from the fact that SimpleDateFormat can not handle ISO8601 format. Here is my little trick to solve this nuisance.

    Create a list of Know format you know that you will use for your application and apply SimpleDateFormat to the list. Now, in your formatDate() method, simple try all your known format and trap the Exception, then if still did not have a date, just use

    Date d = javax.xml.bind.DatatypeConverter.parseDateTime("2013-06-28T00:00:00+00:00").getTime();
    if (d == null)
    try {
        SimpleDateFormater ...
    }
    

    to try it and see if that work. For more info Simple trick to convert Date format with timezone in Java!

提交回复
热议问题