2

I want to parse a date that is sent to me like this..

2011-03-02T09:06:07.404-07:00

The problem is when using the SimpleDateFormat object, I get a parse exception and I'm fairly sure its because of the colon in the timezone.

Here is my setup of SimpleDateFormat.

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");

And here is the list of possible variances in parseable date strings from the developer documentation. As you can see, none of them have a colon in the timezone.

                     yyyy-MM-dd 1969-12-31
                     yyyy-MM-dd 1970-01-01
               yyyy-MM-dd HH:mm 1969-12-31 16:00
               yyyy-MM-dd HH:mm 1970-01-01 00:00
              yyyy-MM-dd HH:mmZ 1969-12-31 16:00-0800
              yyyy-MM-dd HH:mmZ 1970-01-01 00:00+0000
       yyyy-MM-dd HH:mm:ss.SSSZ 1969-12-31 16:00:00.000-0800
       yyyy-MM-dd HH:mm:ss.SSSZ 1970-01-01 00:00:00.000+0000
     yyyy-MM-dd'T'HH:mm:ss.SSSZ 1969-12-31T16:00:00.000-0800
     yyyy-MM-dd'T'HH:mm:ss.SSSZ 1970-01-01T00:00:00.000+0000

My second problem is when I get a date string where the timezone is set to Z. This is the standard for setting a timezone as GMT which is the equivalent of 0000. However I get a ParseException again. Here is an example of the date string.

2011-01-14T10:50:31.520Z

EDIT

Here is how I parse my date string..

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
Date date = null;
try {
    Log.d("CCDateUtilss", "Need to remove the colon from the date string in the timeszone");
    date = sdf.parse(string);
} catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
return date.getTime();

Possible Solution

Could I just go through and replace the problem occurrences in the String so the it parses properly or is there a more elegant solution I don't know about?

Thanks in advance

2
  • I am able to parse it, Its giving me Date like, Wed Mar 02 16:06:07 GMT+00:00 2011. Commented Nov 20, 2012 at 11:40
  • please look at my addition of how I parse my date Commented Nov 20, 2012 at 11:53

1 Answer 1

3

I use the following approcah to parse text dates from diferent sources which may return different formats (including timezone with colon):

private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");


private long getTime(String time) throws Exception {
    try {
        return this.format.parse(time).getTime();
    } catch (Exception e) {
        format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
    }
    try {
        return this.format.parse(time).getTime();
    } catch (Exception e) {
        format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
        //For this you may need to manually adjust time offset
    }
    try {
        return this.format.parse(time).getTime();
    } catch (Exception e) {
        format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ssZ");
    }
    try {
        return this.format.parse(time).getTime();
    } catch (Exception e) {
        format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss'Z'");
        //For this you may need to manually adjust time offset
    }
    return this.format.parse(time).getTime();
}

Note: You may need to adjust manually the time zone offset in case where string ends with Z.

For efficiency, this always try first the last working format.

Regards.

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

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.