4

私は Web サービスを使用しており、ブラジルのフロリアノポリス市では次の日付を取得しています。

2012 年 11 月 6 日(火) 17:30 LST

現在、タイムゾーン「LST」は、SimpleDateFormat パーサーに問題を引き起こします。

// 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);

タイムゾーンの解析が難しいことはわかっています。あなたは何を提案しますか?

ありがとうございました

4

2 に答える 2

2

これを試して

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

私の現在の回避策は次のとおりです。

// 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 に答える