Date now = new Date();
Date then = new Date((long)obj.timestamp*1000);
TimeZone tz = TimeZone.getDefault();
Java にはあまり詳しくありませんが、タイムゾーンをDate
オブジェクトに適用する方法はありますか? このスレッドを見つけましたが、これはカレンダーのタイムゾーンに関するもので、何か違うと思いますか?
Date
オブジェクトはデフォルトで現在のタイムゾーンを使用します。特定のタイムゾーンで時刻を印刷しようとしている場合は、次のように使用できSimpleDateFormat
ます。
//put the desired format
DateFormat formatter= new SimpleDateFormat("MM/dd/yyyy hh:mm:ss Z");
//set the desired timezone
formatter.setTimeZone(TimeZone.getTimeZone("Europe/London"));
String formattedNowInTimeZone = formatter.format(now);
String formattedThenInTimeZone = formatter.format(then);
SimpleDateFormat.setTimeZone(TimeZone)を使用して TimeZoneを設定します。
SimpleDateFormat sdf = new SimpleDateFormat("yourformat");
TimeZone tz = TimeZone.getDefault();
sdf.setTimeZone(tz);
sdf.format(yourdate); //will return a string rep of a date with the included format
Date
オブジェクトにはタイムゾーンがありません。これは単に特定の瞬間のコンテナです。タイムゾーンを適用する場合は、Calendar
. 私は次のようにします
Calendar cal = Calendar.getInstance();
cal.setTime( date );
Date
タイムゾーンに合わせて調整されたものを表示するだけの場合は、 SimpleDateFormatを使用して適切なタイムゾーン調整を適用できます。