Question

I have a date that's in the form of:

Wed Aug 17 2011 09:57:09 GMT+0100 (BST) 

and have a filter that takes a time in a certain format. The problem seems to be the time zone on the end, none of the format strings I'm putting in the filter seem to work for this type of date format.

For example,

Wed Aug 17 2011 09:57:09 GMT+0100 (BST)
EEE MMM dd yyyy HH:mm:ss zZ?

The time zone part of this, keeps throwing an error.

Can anyone tell me what the correct format to parse the time zones on these dates is?

Was it helpful?

Solution

"z" needs a colon between hours and minutes. "Z" is only +/-HHMM (i.e. no "GMT" prefix).

One way to parse it is: EEE MMM dd yyyy HH:mm:ss 'GMT'Z. The "BST" bit is ignored, and it's based on assumption that there's always "GMT" before offset.

OTHER TIPS

I would parse out and interpret the time zone information separately, then use that to construct the Date/Calendar object in the proper time zone.

The following code seems to work well enough with your example:

String source = "Wed Aug 17 2011 09:57:09 GMT+0100 (BST)";
String tzid = "GMT" + source.substring(28, 31)
    + ":" + source.substring(31, 33);
TimeZone tz = TimeZone.getTimeZone(tzid);
// if (tz == null) ?
SimpleDateFormat f = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss");
f.setTimeZone(tz);
Date date = f.parse(source);
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
System.out.println(date);

Prints "Wed Aug 17 08:57:09 UTC 2011".

A more sophisticated approach would be to use regex to extract individual parts ("+/-", "hh" and "mm") of the time zone offset.

Alternatively, you can attempt to discern the 3-letter time zone id (the string in between ( and )), and use the corresponding Java TimeZone if it exists.

In your particular example, though, "BST" resolves to Bangladesh Time which is GMT+0600 so you're better off with the numeric offset. "BST" here should probably be taken as British Summer Time (GMT+0100). This can be important because numeric offsets do not indicate the use of daylight savings time, which can be in effect depending on the date.

A more heuristic routine could take this into account and attempt to resolve the name first, but verify that the GMT offsets match, and fallback on the simple "GMT+hh:mm" timezones otherwise.

If you can not find a pattern matching your use case, try:

try{
    new Date("Wed Aug 17 2011 09:57:09 GMT+0100 (BST)")
}catch(Exception e)
{
  // Parse exception
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top