Java ParseException while attempting String to Date parsing

与世无争的帅哥 提交于 2019-12-04 04:33:10

问题


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 Date String:

2011-10-05T03:00:00Z

Exception:

W/System.err(10072): java.text.ParseException: Unparseable date: "2011-10-05T05:00:00Z" (at offset 10)
W/System.err(10072):    at java.text.DateFormat.parse(DateFormat.java:626)

Sample Code:

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:SSSS");
Date date = formatter.parse(info.AiringTime);

I've found that if I remove the "T" between the date and the time and replace it with a space, it will format just fine. Anybody have any suggestions?

--UPDATE--

After looking deeper into the API documentation, I found this:

All response DateTime values are in UTC format. You need to apply the UTC offset to calculate the local time for display.

DateTime is a date-and-time value specified in one of the following formats:

UTC format: YYYY-MM-DDThh:mm:ssZ. For example: 2011-03-15T02:00:00Z.

Local time with an offset: YYYY-MM-DDThh:mm:ss + or - hh:mm (positive or negative offset). For example, for US Pacific time: 2011-03-14T06:00:00 -08:00.

Any suggestions on the UTC format approach?


回答1:


You could try:

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
String dateString = dateString.replace("Z", "GMT+00:00");
Date date = dateFormat.parse(dateString);

The above code should correctly handle the case where a timezone is specified in the date. As Z represents the UTC/GMT timezone it is replaced by GMT so the SimpleDateFormat can interpret it correctly (i would love to know a cleaner way of handling this bit if anyone knows one).




回答2:


Try,

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");



回答3:


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.




回答4:


This worked for me

    SimpleDateFormat isoDateFormat = new SimpleDateFormat("yyyy-mm-dd'T'hh:mm:ss'Z'");
    SimpleDateFormat viewFriendlyDateFormat = new SimpleDateFormat("dd/MMM/yyyy hh:mm:ss aaa");
    String viewFriendlyDate = "";
    try {
        Date date = isoDateFormat.parse(timestamp);
        viewFriendlyDate = viewFriendlyDateFormat.format(date);

    } catch (ParseException e) {
        e.printStackTrace();
    }



回答5:


SimpleDateFormat isoDateFormat = new SimpleDateFormat("yyyy-mm-dd'T'hh:mm:ss'Z'");
SimpleDateFormat viewFriendlyDateFormat = new SimpleDateFormat("dd/MMM/yyyy hh:mm:ss aaa");
String viewFriendlyDate = "";
try { 
    Date date = isoDateFormat.parse(timestamp);
    viewFriendlyDate = viewFriendlyDateFormat.format(date);

} catch (ParseException e) { 
    e.printStackTrace(); 
} 


来源:https://stackoverflow.com/questions/7669817/java-parseexception-while-attempting-string-to-date-parsing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!