3

フォーマットされた日付文字列を日付オブジェクトに変換しようとしています。日付文字列が他のタイムゾーンにフォーマットされています。

私が行うsdf.parse(String)と、システム日付オブジェクトが返されます。

コードは以下の通り、

static Date convertGMTTime(String timeZone, long longDate){
    Date convertedTime = null;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    try{
        Date date = new Date(longDate);
        System.out.println("timezone: "+timeZone +", timestamp: "+date);
        Locale locale = Locale.ENGLISH;
        TimeZone destTimeZone = TimeZone.getTimeZone(timeZone);// TimeZone.getDefault();
        System.out.println("Source timezone: "+destTimeZone);
/*          DateFormat formatter = DateFormat.getDateTimeInstance(
                    DateFormat.DEFAULT,
                    DateFormat.DEFAULT,
                    locale);
        formatter.setTimeZone(destTimeZone);*/
        sdf.setTimeZone(destTimeZone);
        String convertedDateStr = sdf.format(date);
        System.out.println("convertedDateStr: "+convertedDateStr);
        convertedTime = sdf.parse(convertedDateStr);
        System.out.println("convertedTime: "+convertedTime + "sdf: "+sdf.getTimeZone());
    }catch(Exception e){
        e.printStackTrace();
    }
    return convertedTime;
}

誰かが助けて、私が間違っているところを指摘してくれれば幸いです。前もって感謝します。

出力:

タイムゾーン: Atlantic/Cape_Verde、タイムスタンプ: Tue Jun 26 17:38:11 IST 2012
ソースタイムゾーン: sun.util.calendar.ZoneInfo[id="Atlantic/Cape_Verde",offset=-3600000,dstSavings=0,useDaylight=false,トランジション=6,lastRule=null]

変換されたDateStr: 2012-06-26 11:08:11

変換時間: 火曜日 6 月 26 日 17:38:11 IST 2012
sdf:sun.util.calendar.ZoneInfo[id="Atlantic/Cape_Verde",offset=-3600000,dstSavings=0,useDaylight=false,transitions=6,lastRule=null ]


共有する詳細がいくつかあります。別の sdf オブジェクトを (タイムゾーンを設定せずに) 使用すると、正しい時刻と日付が返されますが、タイムゾーンはシステムクロックから選択されます

コード

static Date convertGMTTime(String timeZone, long longDate){
    Date convertedTime = null;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    SimpleDateFormat sdfParse = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    try{
        Date date = new Date(longDate);
        TimeZone destTimeZone = TimeZone.getTimeZone(timeZone);// TimeZone.getDefault();
        System.out.println("Source timezone: "+destTimeZone);
        sdf.setTimeZone(destTimeZone);
        String convertedDateStr = sdf.format(date);
        System.out.println("convertedDateStr: "+convertedDateStr );
        convertedTime = sdfParse.parse(convertedDateStr,new ParsePosition(0));
        System.out.println("convertedTime: "+convertedTime + "sdf: "+sdf.getTimeZone());
    }catch(Exception e){
        e.printStackTrace();
    }
    return convertedTime;
}

出力

Source timezone: sun.util.calendar.ZoneInfo[id="Atlantic/Cape_Verde",offset=-3600000,dstSavings=0,useDaylight=false,transitions=6,lastRule=null]

convertedDateStr: 2012-06-26 12:24:56

convertedTime: Tue Jun 26 12:24:56 IST 2012 

sdf: sun.util.calendar.ZoneInfo[id="Atlantic/Cape_Verde",offset=-3600000,dstSavings=0,useDaylight=false,transitions=6,lastRule=null]

タイムゾーンを sdf に割り当てないとシステム タイム ゾーンが使用されることは理解していますが、システム タイム ゾーンで時間が表示されないのはなぜですか? 文字列と同じようにタイムゾーンで表示しますが、タイムゾーンが異なります。

回答タイムゾーンを設定すると、sdfに他のタイムゾーンが設定されているという事実に関係なく、システム時間に従って日付オブジェクトが返されます。

sdf.parse と sdf.format の機能的な動作を説明してください。

私にとって、sdf.setTimeZone() は format を使用すると影響を与え、sdf.parse() を使用すると無効になります。かなり奇妙だと思います。

この点で助けに感謝します。

4

1 に答える 1

7

既に Date (または Date のミリ秒数) があるため、変換するものはありません。Date にはタイムゾーンがありません。それは普遍的な瞬間です。日付 65647678000 は、あるタイム ゾーンでは 12:38 であるが、別のタイム ゾーンでは 10:38 である可能性があるため、タイム ゾーンはこの日付を表示する場合にのみ関連します。10:38 は、あるタイム ゾーンでは 65647678000 ですが、別のタイム ゾーンでは 65657678000 であるため、日付の文字列表現を解析する場合にも関連します。

Date オブジェクトを表示したり、String を Date に解析したりしませんが、タイム ゾーンを気にする必要はありません。表示/解析時に使用するタイム ゾーンを選択するには、 のタイム ゾーンを設定し、/をDateFormat使用して日付をフォーマット/解析します。DateFormat.format()DateFormat.parse()

を使用Date.toString()して日付を表示すると、常に現在のタイム ゾーンが使用されます。

日付を日、月、年、時間などではなく、「ケネディが撃たれたとき」の瞬間として考えると、私が何を意味するのか理解しやすくなります。「ケネディが撃たれた瞬間」は誰にとっても同じ瞬間です。しかし、「ケネディが撃たれた」瞬間をダラス タイム ゾーンで表現した場合、この同じ瞬間をパリ タイム ゾーンで表現したときに得られる結果とは異なります。

于 2012-06-26T12:39:41.157 に答える