0

ロケール時間を UTC に変換してから、UTC をロケール時間に変換しようとしています。しかし、私は結果を得ていません。

public class DateDemo
{

public static void main(String args[])
{
    DateFormat dateFormatter = 
               DateFormat.getDateTimeInstance
                 (DateFormat.SHORT, DateFormat.SHORT, Locale.getDefault());

    TimeZone.setDefault(TimeZone.getDefault());

    SimpleDateFormat simpleDateFormatter = new SimpleDateFormat("dd/MM/yyyy");
    SimpleDateFormat simpleTimeFormatter = new SimpleDateFormat("hh:mm:ss a");

    Date today = new Date();
    String localeFormattedInTime = dateFormatter.format(today);

    try
    {
        Date parsedDate = dateFormatter.parse(localeFormattedInTime);
        System.out.println("Locale:" + localeFormattedInTime);
        System.out.println("After parsing a date: " + parsedDate);

        simpleDateFormatter.setTimeZone(TimeZone.getDefault());
        simpleTimeFormatter.setTimeZone(TimeZone.getDefault());

        String date = simpleDateFormatter.format(today);
        String time = simpleTimeFormatter.format(today);
        System.out.println("Today's only date: " + date);
        System.out.println("Today's only time: " + time);

        //// Locale to UTC converting

        simpleDateFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
        simpleTimeFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));

        String utcDate = simpleDateFormatter.format(today);
        String utcTime = simpleTimeFormatter.format(today);
        System.out.println("Convert into UTC's date: " + utcDate);
        System.out.println("Convert into UTC's only time: " + utcTime);

         //// UTC to locale converting

        simpleDateFormatter.setTimeZone(TimeZone.getDefault());
        simpleTimeFormatter.setTimeZone(TimeZone.getDefault());

        Date getDate = simpleDateFormatter.parse(utcDate);
        Date getTime = simpleTimeFormatter.parse(utcTime);

        String getLocalDate = simpleDateFormatter.format(getDate);
        String getLocalTime = simpleTimeFormatter.format(getTime);
        System.out.println("Get local date: " + getLocalDate);
        System.out.println("Get local time: " + getLocalTime);

    } catch (ParseException e) {
        e.printStackTrace();
    }
  }
 }

ローカルの日付と時刻を Web サービスに送信していますが、必要に応じて UTC の日付と時刻を取得し、ロケールの日付と時刻 (つまり、ユーザーのローカル設定) に変換する必要があります。

サンプル出力:

Locale:11/9/12 8:15 PM
After parsing a date: Fri Nov 09 20:15:00 SGT 2012
Today's only date: 09/11/2012
Today's only time: 08:15:30 PM
Convert into UTC's date: 09/11/2012
Convert into UTC's only time: 12:15:30 PM
Get local date: 09/11/2012
Get local time: 12:15:30 PM

Saksak と ADTC が回答した後:

コードフラグメントの場合、UTC の日付と時刻 (データベースが米国にある可能性があるため、実際にはGMT-5として表示されるもの) が入力であり、ローカルの日付と時刻を出力として取得したいと考えています。しかし、この次のセグメントはまだGMT-5時間を提供しています。

SimpleDateFormat simpleDateTimeFormatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");

....

Date inDateTime = simpleDateTimeFormatter.parse(intent.getExtras().getString("inTime")); Date outDateTime = simpleDateTimeFormatter.parse(intent.getExtras().getString("outTime"));

simpleDateTimeFormatter.setTimeZone(TimeZone.getDefault()); simpleDateTimeFormatter.setTimeZone(simpleDateTimeFormatter.getTimeZone());

//TimeZone tzTimeZone = TimeZone.getDefault();

//System.out.println("Current time zone: " + tzTimeZone.getDisplayName());

String getLocalInTimeString = simpleDateTimeFormatter.format(inDateTime); 

String getLocalOutTimeString = simpleDateTimeFormatter.format(outDateTime);

私の質問: getLocalInTimeString&getLocalOutTimeStringまだGMT-5タイミングを表示しています。ここで何が問題なのですか?他に設定する必要はありますか?

4

2 に答える 2

4

あなたの問題は54行目と55行目にあります。

    Date getDate = simpleDateFormatter.parse(utcDate);
    Date getTime = simpleTimeFormatter.parse(utcTime);

これらの行は、日付と時刻を含む文字列を解析しているだけですが、これらの文字列にはタイムゾーン情報がありません。

    utcDate = "09/11/2012"
    utcTime = "12:15:30 PM"

したがって、パーサーは、文字列が51行目と52行目で設定したタイムゾーンのロケールにすでに存在していると想定します。

次に、それを修正する方法を考えてください;)ヒント:パーサーが文字列で表される時間の正しいタイムゾーンを想定していることを確認してください。

PS:[解決済み!] 問題は解決しましたが、少なくとも私がいる場所では、タイムゾーンの変換が不安定であることがわかりました。時間は8:30 pmローカルです。UTCに変換し12:30 pmます(正しい、8時間の差)。逆に変換します8:00 pm(間違っています。設定されたタイムゾーンは正しいですが、元のタイムゾーンを取得して返しましたが、 7.5時間の差しかありません)。何が起こっているのか、そしてそれをどのように解決するのかを理解できない限り、より信頼できる方法を探す必要があります。

[解決策:]上記の問題は、実際には、元のコードが日付と時刻を2つの異なるパーサーに分割していたことが原因でした。日付と時刻の両方を組み合わせて1つのパーサーのみを使用する場合は、ターゲットロケールで正しい日付と時刻を取得します。したがって、結論として、パーサー信頼できますが、使用方法によって大きな違いが生じます。

    SimpleDateFormat simpleDateTimeFormatter
            = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");
    Date getDateTime
            = simpleDateTimeFormatter.parse(utcDate + " " + utcTime);
    //use above line if you have the date and time as separate strings
    simpleDateTimeFormatter.setTimeZone(TimeZone.getDefault());
    String getLocalDateTime = simpleDateTimeFormatter.format(getDateTime);
    System.out.println("Get local date time: " + getLocalDateTime);

日付と時刻に2つの別々のパーサーを使用することが信頼できない理由:
上記で説明したように、日付と時刻の部分に2つの別々のパーサーを使用することはお勧めできません。理由は次のとおりです。

    Date getDate = simpleDateFormatter.parse(utcDate);
    Date getTime = simpleTimeFormatter.parse(utcTime);
    //Time zone changed to local here
    String getLocalDate2 = simpleDateTimeFormatter.format(getDate);
    String getLocalTime2 = simpleDateTimeFormatter.format(getTime);
    System.out.println("Get local date2: " + getLocalDate2);
    System.out.println("Get local time2: " + getLocalTime2);

    OUTPUT:
    Get local date2: 10/11/2012 08:00:00 AM
    Get local time2: 01/01/1970 10:35:10 AM

変数の保存時間(2行目)でデフォルトの日付01/01/1970が使用されているため、30分差があります。Dateこれをローカルタイムゾーンに変換すると、フォーマッタがデフォルトの日付に基づいて変換を行うため、エラーが発生します01/01/1970 私が住んでいる場所では、1970年の時差は+7.5時間でしたが、今日は+8時間です)これが、正しい結果が得られたとしても2つの別々のパーサーが信頼できない理由であり、日付と時刻の両方の情報を受け入れる組み合わせパーサーを常に使用する必要があります。

于 2012-11-09T12:23:20.310 に答える
4

問題を解決するために必要なことは次のとおりです。コードをこの順序で現地時間に戻す必要があります。

simpleDateFormatter.setTimeZone(TimeZone.getDefault());
simpleTimeFormatter.setTimeZone(TimeZone.getDefault());

Date getDate = simpleDateFormatter.parse(utcDate);
Date getTime = simpleTimeFormatter.parse(utcTime);

必要なことは、utcDate、utcTime 文字列を解析して Date オブジェクトに戻すまで待ってから、次のように日付フォーマッタのタイム ゾーンをローカル ゾーンに設定することです。

Date getDate = simpleDateFormatter.parse(utcDate);
Date getTime = simpleTimeFormatter.parse(utcTime);

simpleDateFormatter.setTimeZone(TimeZone.getDefault());
simpleTimeFormatter.setTimeZone(TimeZone.getDefault());

これにより、正しい日付/時刻がローカルに出力されます。

編集:ここに完全なメインメソッドがあります:

public static void main(String[] args) {
    DateFormat dateFormatter = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, Locale.getDefault());
    TimeZone.setDefault(TimeZone.getDefault());
    SimpleDateFormat simpleDateFormatter = new SimpleDateFormat("dd/MM/yyyy");
    SimpleDateFormat simpleTimeFormatter = new SimpleDateFormat("hh:mm:ss a");
    Date today = new Date();
    String localeFormattedInTime = dateFormatter.format(today);
    try {
        Date parsedDate = dateFormatter.parse(localeFormattedInTime);
        System.out.println("Locale:" + localeFormattedInTime);
        System.out.println("After parsing a date: " + parsedDate);

        simpleDateFormatter.setTimeZone(TimeZone.getDefault());
        simpleTimeFormatter.setTimeZone(TimeZone.getDefault());

        String date = simpleDateFormatter.format(today);
        String time = simpleTimeFormatter.format(today);
        System.out.println("Today's only date: " + date);
        System.out.println("Today's only time: " + time);

        //// Locale to UTC converting

        System.out.println("TimeZone.getDefault() >>> " + TimeZone.getDefault());

        simpleDateFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
        simpleTimeFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));

        String utcDate = simpleDateFormatter.format(today);
        String utcTime = simpleTimeFormatter.format(today);
        System.out.println("Convert into UTC's date: " + utcDate);
        System.out.println("Convert into UTC's only time: " + utcTime);

        //// UTC to locale converting
        /**
         ** //////EDIT
        */
        // at this point your utcDate,utcTime are strings that are formated in UTC
        // so first you need to parse them back to Dates using UTC format not Locale
        Date getDate = simpleDateFormatter.parse(utcDate);
        Date getTime = simpleTimeFormatter.parse(utcTime);

        // NOW after having the Dates you can change the formatters timezone to your
        // local to format them into strings
        simpleDateFormatter.setTimeZone(TimeZone.getDefault());
        simpleTimeFormatter.setTimeZone(TimeZone.getDefault());

        String getLocalDate = simpleDateFormatter.format(getDate);
        String getLocalTime = simpleTimeFormatter.format(getTime);
        System.out.println("Get local date: " + getLocalDate);
        System.out.println("Get local time: " + getLocalTime);

    } catch (ParseException e) {
        e.printStackTrace();
    }

}
于 2012-11-09T12:31:23.523 に答える