hooked82
hooked82

Reputation: 6376

Java ParseException while attempting String to Date parsing

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?

Upvotes: 3

Views: 8738

Answers (5)

ik024
ik024

Reputation: 3594

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(); 
} 

Upvotes: 0

ik024
ik024

Reputation: 3594

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();
    }

Upvotes: 0

Rob
Rob

Reputation: 4753

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).

Upvotes: 12

Paul Jackson
Paul Jackson

Reputation: 2147

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.

Upvotes: 1

KV Prajapati
KV Prajapati

Reputation: 94653

Try,

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

Upvotes: 2

Related Questions