Reputation: 812
I am using a web service and for the city Florianopolis in Brazil I get the following date:
Tue, 06 Nov 2012 5:30 pm LST
Now the timezone "LST" creates a problem to the SimpleDateFormat parser:
// Date to parse
String dateString = "Tue, 06 Nov 2012 5:30 pm LST";
// This parser works with other timezones
SimpleDateFormat LONG_DATE = new SimpleDateFormat("EEE, d MMM yyyy h:mm a zzz");
// Here it throws a ParseException
Date date = LONG_DATE.parse(dateString);
I know that the timezones can be difficult to parse. What do you propose?
Thank you
Upvotes: 4
Views: 651
Reputation: 812
My current workaround is:
// Date to parse
String dateString = "Tue, 06 Nov 2012 5:30 pm LST";
// This parser works with some timezones but fails with ambiguous ones...
DateFormat dateFormat = new SimpleDateFormat("EEE, d MMM yyyy h:mm a zzz");
Date date = null;
try {
// Try to parse normally
date = dateFormat.parse(dateString);
} catch (ParseException e) {
// Failed, try to parse with a GMT timezone as a workaround.
// Replace the last 3 characters with "GMT"
dateString = dateString.replaceFirst("...$", "GMT");
// Parse again
date = dateFormat.parse(dateString);
}
Upvotes: -1
Reputation: 1568
Try this
DateFormat gmtFormat = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss Z");
TimeZone gmtTime = TimeZone.getTimeZone("GMT-02:00");
gmtFormat.setTimeZone(gmtTime);
System.out.println("Brazil :: " + gmtFormat.format(new Date()));
Upvotes: 2