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?

hooked82
  • 6,026
  • 4
  • 38
  • 44

5 Answers5

11

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

shridutt kothari
  • 7,495
  • 2
  • 41
  • 59
Rob
  • 4,593
  • 3
  • 29
  • 29
  • 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? – hooked82 Oct 06 '11 at 03:36
  • Cancel that, figured it out. Thanks! – hooked82 Oct 06 '11 at 03:53
  • 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); – Rob Oct 06 '11 at 03:58
  • Can you explain here in "String dateString =info.AiringTime.replace("Z", "GMT+00:00")"; what is info? – Chirag Parmar Oct 18 '14 at 12:36
2

Try,

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
kv-prajapati
  • 90,019
  • 18
  • 141
  • 178
  • 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? – hooked82 Oct 06 '11 at 03:29
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.

Community
  • 1
  • 1
Paul Jackson
  • 1,847
  • 17
  • 25
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();
    }
ik024
  • 3,328
  • 5
  • 31
  • 58
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(); 
} 
ik024
  • 3,328
  • 5
  • 31
  • 58