2011-12-13 17 views
1

私はDateオブジェクトとタイムゾーンをTimeZoneオブジェクトとしてタイムスタンプを保存します。java日付をタイムゾーンに基づいて日付オブジェクトに変換するにはどうすればよいですか?

ここでは、DateオブジェクトとTimeZoneオブジェクトを引数として、タイムスタンプを使用して調整されたDateオブジェクトを返す関数を作成します。例えば

入力:

 
Date TimeZone 

12:00 Moscow Standard Time (UTC+3) 

出力:

 
Date 

3:00  

編集: 削除注意についてCalendar

+0

カレンダーはこれを行う方法です。あなたのユースケースは、問題を解決するために使用する内部オブジェクトと何が関係していますか? – Thom

+0

私が意味することは、私はdbでカレンダーオブジェクトを保存したくないということです。あなたはカレンダーを使用してそれを変換する方法の例を提供できますか? – Jimmy

+0

ああジョーダの時間は人生をはるかに簡単になります。 – Andy

答えて

7

java.util.Dateは、絶対的な時点です。 0900時間UTCおよび1200時間UTC + 3は全く同じjava.util.Dateオブジェクトです。どちらか一方を表現するために、それに加えられる「調整」はありません。

特定のタイムゾーンを人間が判読できる表現にするには、DateFormatオブジェクトにタイムゾーンを設定します。

DateFormat format = new SimpleDateFormat("HH:mm"); 
format.setTimeZone(TimeZone.getTimeZone("UTC+3")); 
String time = format.format(yourDate); 

コメントで提起質問のためのソリューション:

Calendar cal1 = Calendar.getInstance(TimeZone.getTimeZone("UTC+3")); 
cal1.setTime(yourDate); 
Calendar cal2 = Calendar.getInstance(TimeZone.getTimeZone("UTC")); 
cal2.clear(); 
cal2.set(Calendar.YEAR, cal1.get(Calendar.YEAR)); 
cal2.set(Calendar.MONTH, cal1.get(Calendar.MONTH)); 
cal2.set(Calendar.DATE, cal1.get(Calendar.DATE)); 
cal2.set(Calendar.HOUR_OF_DAY, cal1.get(Calendar.HOUR_OF_DAY)); 
//simile for whatever level of field precision is needed 
Date shiftedDate = cal2.getTime(); 
+0

私はタイムゾーン(UTC + 3)に基づいて絶対的なポイントをシフトしたいので、私は0900時間UTCを持っていると言うことができます私はそれを1200時間 "UTC"にし、そのタイムスタンプをストリングではないようにします – Jimmy

+0

あなたは入力を生成しているコードにアクセスすることができます。私は個人的には入力パーサーを修正することを非常に好むでしょう。いずれにせよ、あなたが尋ねたことをやる方法はここにあります。 :) – Affe

+0

ありがとう、しかし私は簡単な方法を見つけた、私の答えを見てください。 – Jimmy

0

あなたがCを使用していない場合alendar、あなたは何を使っていますか?おそらく別の図書館?

もしそうでなければ、タイムゾーンの終わりにそこに+3があります。それを使って時間(+/-)Xをバンプすることができます。この場合、+3。この場合(例えば)11 + 3 = 2ということだけを覚えておいてください。この計算は、時間+オフセットを追加し、その値%12をとり、その答えを時間として設定することで達成できます(必要に応じて0〜12の回答を設定します)。それは理にかなっていますか?

+0

偉大な、質問は私の下から編集されました。この答えは無視してください。 –

+0

私が意味するものは、dbのカレンダーオブジェクトを保存したくないということです。私はそのメモを削除しました。私はそれを変換するカレンダーを使用することができます。あなたは例を挙げることができますか? – Jimmy

0

は、私は希望の時間帯のrowOffsetを使用していることを実現するための簡単な方法を見つけました:

Date date = new Date(); 
int rawOffset = TimeZone.getTimeZone("EST").getRawOffset(); 
Date adjustedDate = new Date(date.getTime() + rawOffset) 

編集:これは、閏秒と夏時間を無視することを示しています。

+1

この方法は閏秒で厳密に正しいわけではありませんし、夏時間のスイッチでは、言葉それが適切な場合は、それをどのように使用しているかによって決まります。 – Affe

+0

良い点。私の答えはうるう秒や夏時間を考慮しません。 – Jimmy

1

ここに行く:

/** 
* Convert a calendar from its current time zone to UTC (Greenwich Mean Time) 
* @param local the time 
* @return a calendar with the UTC time 
*/ 
public static Calendar convertTimeToUtc(Calendar local){ 
    int offset = local.getTimeZone().getOffset(local.getTimeInMillis()); 
    GregorianCalendar utc = new GregorianCalendar(TZ_UTC); 
    utc.setTimeInMillis(local.getTimeInMillis()); 
    utc.add(Calendar.MILLISECOND, -offset); 

    return utc; 
} 

/** 
* Convert a UTC date into the specified time zone 
* @param tzName the name of the time zone for the output calendar 
* @param utc the UTC time being converted 
* @return a calendar in the specified time zone with the appropriate date 
*/ 
public static Calendar convertTimeToLocal(String tzName, Calendar utc) { 
    TimeZone zone = TimeZone.getTimeZone(tzName); 
    int offset = zone.getOffset(utc.getTimeInMillis()); 
    GregorianCalendar local = new GregorianCalendar(zone); 
    local.setTimeInMillis(utc.getTimeInMillis()); 
    local.add(Calendar.MILLISECOND, offset); 

    return local; 
} 

/** 
* Convert a UTC date into the specified time zone 
* @param zone the time zone of the output calendar 
* @param utc the UTC time being converted 
* @return a calendar in the specified time zone with the appropriate date 
*/ 
public static Calendar convertTimeToLocal(TimeZone zone, Calendar utc) { 
    int offset = zone.getOffset(utc.getTimeInMillis()); 
    GregorianCalendar local = new GregorianCalendar(zone); 
    local.setTimeInMillis(utc.getTimeInMillis()); 
    local.add(Calendar.MILLISECOND, offset); 

    return local; 
} 
関連する問題