TimeZoneを考慮した日付のフォーマット
Calendarクラスを使うときあまりTimeZoneを意識した事が無かったのでTimeZoneを意識して作ってたメモ。
package jp.co.suusuke; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import java.util.TimeZone; /** * @author suusuke * */ public class TimeZoneSample { /** * */ private static final String YYYYMMDDHHMMSS = "yyyyMMdd/HHmmss"; /** * */ public Calendar c = new GregorianCalendar(2008, 6, 22, 0, 0, 0); /** * @param args * @throws ParseException */ public static void main(String[] args) throws ParseException { TimeZoneSample tzs = new TimeZoneSample(); System.out.println("default:" + DateUtil.convertDefault(tzs.c.getTime())); System.out.println("GMT :" + DateUtil.convertGMT(tzs.c.getTime())); System.out.println("UTC :" + DateUtil.convertUTC(tzs.c.getTime())); System.out.println("JST :" + DateUtil.convertJST(tzs.c.getTime())); } /** * @author suusuke * */ public static class DateUtil { /** * @param date * @return */ public static String convertDefault(Date date) { DateFormat df = new SimpleDateFormat(YYYYMMDDHHMMSS); df.setTimeZone(TimeZone.getDefault()); return df.format(date); } /** * @param date * @return */ public static String convertGMT(Date date) { DateFormat df = new SimpleDateFormat(YYYYMMDDHHMMSS); df.setTimeZone(TimeZone.getTimeZone("GMT")); return df.format(date); } /** * @param date * @return */ public static String convertUTC(Date date) { DateFormat df = new SimpleDateFormat(YYYYMMDDHHMMSS); df.setTimeZone(TimeZone.getTimeZone("UTC")); return df.format(date); } /** * @param date * @return */ public static String convertJST(Date date) { DateFormat df = new SimpleDateFormat(YYYYMMDDHHMMSS); df.setTimeZone(TimeZone.getTimeZone("JST")); return df.format(date); } } }
実行結果
default:20080722/000000
GMT :20080721/150000
UTC :20080721/150000
JST :20080722/000000
SimpleDateFormatは抽象クラスであるDateFormatを継承していて、DateFormatのプロパティにCalendarクラスを保持しています。
なので、SimpleDateFormatでは、プロパティにパターンを保持しformatメソッドの引数のDateから日時を取得し、フォーマットを行っている仕組みになっています。
ってことは、TimeZone切り替えるだけでその日付を指定したTimeZoneの時間に変換してくれるのか?
package jp.co.suusuke; import java.util.Calendar; import java.util.TimeZone; public class DateSample { public static void main(String[] args) { Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT")); c.setTimeZone(TimeZone.getTimeZone("GMT")); System.out.println(c.getTime()); System.out.println(c.getTimeZone()); System.out.println(c.get(Calendar.HOUR_OF_DAY)); c.setTimeZone(TimeZone.getTimeZone("JST")); System.out.println(c.getTime()); System.out.println(c.getTimeZone()); System.out.println(c.get(Calendar.HOUR_OF_DAY)); } }
結果
Sun Jun 22 15:01:42 JST 2008
sun.util.calendar.ZoneInfo[id="GMT",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null]
6
Sun Jun 22 15:01:42 JST 2008
sun.util.calendar.ZoneInfo[id="JST",offset=32400000,dstSavings=0,useDaylight=false,transitions=10,lastRule=null]
15
ん〜、getTime()メソッドだとJSTが返ってくるけど、get()メソッドだと指定したTimeZoneで時間が取れてる。
なんで、getTime()メソッドだとJSTが返ってくるんだろう。
根本的な事が理解できてないような気がする。