ALGOBIT

2010/03/24

Google App Engine で java.util.Date

Filed under: 離散的な気まぐれ — タグ: , — Kohyama @ 01:50


GAE/J では java.util.Date 型を new すると, タイムゾーンがUTC のオブジェクトができます.
タイムゾーン JST で, 文字から Date 型にしたり, Date 型の値を JST で文字列化する場合の覚書.
宣言:

private java.text.SimpleDateFormat sdf;

初期化:

sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
sdf.setTimeZone(TimeZone.getTimeZone("JST"));

JSTの時刻の書かれた String s をパースして Date d へ代入:

d = sdf.parse(s);

Date d を文字列化して String s に代入:

s = sdf.format(d);

Copyright © 2010 Yoshinori Kohyama All Rights Reserved.