2011-12-15 3 views
4

私たちの日付は、エポックからのミリ秒単位で格納され、時間関連のデータを表示するオブジェクトのOlsonタイムゾーンIDから格納されます。OlsonタイムゾーンIDをGWT(クライアント側)のTimeZoneConstantに変換します。

Olson TZIDをTimeZoneConstantに変換してTimeZoneを作成し、DateTimeFormatを使用するにはどうすればよいですか?

// values from database 
String tzid = "America/Vancouver"; 
long date = 1310771967000L; 


final TimeZoneConstants tzc = GWT.create(TimeZoneConstants.class); 
String tzInfoJSON = MAGIC_FUNCTION(tzid, tzc); 
TimeZone tz = TimeZone.createTimeZone(TimeZoneInfo.buildTimeZoneData(tzInfoJSON)); 
String toDisplay = DateTimeFormat.getFormat("y/M/d h:m:s a v").format(new Date(date), tz); 

MAGIC_FUNCTIONは存在しますか?あるいは、これを行う別の方法がありますか?

答えて

4

GWT Javadoc [1]によると、TimeZoneConstantsクラスでGWT.createを実行するのは悪いゲームです。ですから、私がやったのは、サーバー側で/com/google/gwt/i18n/client/constants/TimeZoneConstants.propertiesを解析し、各タイムゾーンのすべてのJSONオブジェクトのキャッシュを構築するクラスを作成することでした(Olson TZID )。

私のサイトはjboss上で実行されていますので、TimeZoneConstants.propertiesを自分のサイトのwar/WEB-INF/libディレクトリにコピーしました(GWT jarsが既にそこにあるので、

InputStream inStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(PROPERTIES_FILE); 
InputStreamReader isr = new InputStreamReader(inStream); 
BufferedReader br = new BufferedReader(isr); 
for (String s; (s = br.readLine()) != null;) { 
    // using a regex to grab the id to use as a key to the hashmap 
    // a full json parser here would be overkill 
    Pattern pattern = Pattern.compile("^[A-Za-z]+ = (.*\"id\": \"([A-Za-z_/]+)\".*)$"); 
    Matcher matcher = pattern.matcher(s);  
    if (matcher.matches()) { 
    String id = matcher.group(2); 
    String json = matcher.group(1); 

    if (!jsonMap.containsKey(id)) { 
     jsonMap.put(id, json); 
    } 
    } 
} 
br.close(); 
isr.close(); 
inStream.close(); 

最後に、私は(私が興味TimeZoneID知っているサーバーを想定して)クライアントにTimeZoneInfoJSONを取得するために、RPC呼び出しを行います。そして、私が構築したときに解析を行いシングルトンクラスを持っています

getTimeZone(new PortalAsyncCallback<String>() { 
    public void onSuccess(String tzJson) { 
    timeZone = TimeZone.createTimeZone(TimeZoneInfo.buildTimeZoneData(tzJson)); 
    } 
}); 

最も洗練された解決策ではありませんが、DSTの移行を超える特定のタイムゾーンの日付と時刻を表示する方法を提供しています。

[1] http://google-web-toolkit.googlecode.com/svn/javadoc/2.0/com/google/gwt/i18n/client/constants/TimeZoneConstants.html

+0

これはとても不自然なようですか? (私がコピーしようとしているものではなく、gwtの部分に) – NimChimpsky

関連する問題