Java ParseException while attempting String to Date parsing

前端 未结 5 1086
生来不讨喜
生来不讨喜 2021-01-20 08:32

I\'m having a hard time Parsing/Formatting a Date string received back from a web service. I\'ve attempted multiple approaches, but with no luck.

Sample Dat

5条回答
  •  感动是毒
    2021-01-20 09:26

    This pattern should parse the date you provide: "yyyy-MM-dd'T'HH:mm:ss'Z'".
    If you want to use SimpleDateFormat and you have a limited number of variations, you can create separate formatters for each pattern and chain them:

    Date date = formatter1.parse(info.AiringTime);
    if (date == null)
    {
      date = formatter2.parse(info.AiringTime);
      if (date == null)
      {
        date = formatter2.parse(info.AiringTime);
        if (date == null)
        {
          date = formatter3.parse(info.AiringTime);
        }
      }
    }
    

    or put them in a list and iterate until non-null or no more formatters.
    If you have too many patterns for this to be practical, you can parse it yourself or try one of these libraries.

提交回复
热议问题