4

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

4

2 回答 2

2

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()));
于 2012-11-07T10:40:40.233 回答
0

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);
}
于 2012-11-07T11:20:43.787 回答