3

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?

6
  • If you know it has a T in there, you could just replace it before feeding it in... Commented Oct 6, 2011 at 2:42
  • I had thought about that, but the API does not confirm that t is in every response Commented Oct 6, 2011 at 2:54
  • You could check for it with an if statement... Commented Oct 6, 2011 at 2:59
  • Z is a valid time value (it represents the UTC timezone in ISO8061/RFC3339 formatted dates) Commented Oct 6, 2011 at 3:14
  • The answer I supplied should handle both the UTC format and local time format sent that API Commented Oct 6, 2011 at 3:30

5 Answers 5

12

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

Sign up to request clarification or add additional context in comments.

4 Comments

Excellent, this does work. So let's say that what I wanted the final output to be for example: 9:30 PM, where in that code should I do another format?
Cancel that, figured it out. Thanks!
Awesome, just be a little careful of how you format for display, generally you want to let the system handle the formatting so it displays correctly in other locales (such as France etc). e.g. use something like String myDateStr = DateFormat.getDateInstance(DateFormat.SHORT).format(date);
Can you explain here in "String dateString =info.AiringTime.replace("Z", "GMT+00:00")"; what is info?
2

Try,

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

1 Comment

This seems to have worked, but the date didn't output as I would have thought. It came out as: Thu Oct 06 05:00:00 PDT 2011. Should I just run another Format over this to get my desired output?
1

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.

Comments

0

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.