2

JodaTimeライブラリを使用して現地時間を取得するのに問題があります。これが私がやろうとしていることの簡単な例です。

package simple;

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.LocalDateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

public class TestJodaLocalTime {

    public static void main(String[] args) {
        //time in UTC standard
        DateTime processingDate = DateTime.now();
        //local time
        LocalDateTime localDateTime = processingDate.
                withZone(DateTimeZone.getDefault()).toLocalDateTime();

        DateTimeFormatter fmtDate = DateTimeFormat.forPattern("dd/MM/yyyy");
        DateTimeFormatter fmtHour = DateTimeFormat.forPattern("HH:mm:ss");

        System.out.println("Date: " + fmtDate.print(processingDate));
        System.out.println("Time: " + fmtHour.withZone(DateTimeZone.UTC).
                print(processingDate));
        System.out.println("Local date: " + fmtDate.print(localDateTime));
        System.out.println("Local time: " + fmtHour.print(localDateTime));

    }

}

現地時間を印刷するときに、アプリケーションが実行されているタイムゾーンの時刻を取得することを期待していましたが、UTCDateTimeと同じ時刻を取得しました。

Date: 06/12/2012
Time: 12:55:49
Local date: 06/12/2012
Local time: 12:55:49

私はここで何を逃しましたか?

4

2 に答える 2

1

以下を試すことができます (私は GMT+2:00 タイムゾーンにいます):

public class TestJodaLocalTime {

    public static void main(String[] args) {
        DateTime utcNow = DateTime.now(DateTimeZone.UTC);
        DateTime localNow = utcNow.withZone(DateTimeZone.getDefault());

        DateTimeFormatter fmt = ISODateTimeFormat.dateHourMinuteSecond();

        System.out.println("UTC   now: " + fmt.print(utcNow));
        System.out.println("Local now: " + fmt.print(localNow));
    }

}

出力:

UTC   now: 2012-12-07T17:43:43
Local now: 2012-12-07T19:43:43
于 2012-12-07T17:46:35.577 に答える
1

このコメントは問題の一部である可能性があります:

//time in UTC standard
DateTime processingDate = DateTime.now();

いいえ、DateTime.now()デフォルトのタイムゾーンで現在の時刻を返します。したがって、後で使用するときwithZone(DateTimeZone.getDefault())はノーオペレーションです。

fmtHour.withZone(DateTimeZone.UTC).print(processingDate)ただし、 UTCに変換することはまだ期待していました。

実際、デフォルトのタイム ゾーンを UTC 以外に手動で設定すると、次のようになります。

import org.joda.time.*;
import org.joda.time.format.*;

public class Test {
    public static void main(String[] args) {
        DateTimeZone pacific = DateTimeZone.forID("America/Los_Angeles");
        DateTimeZone.setDefault(pacific);

        DateTime processingDate = DateTime.now();
        //local time
        LocalDateTime localDateTime = processingDate.
            withZone(DateTimeZone.getDefault()).toLocalDateTime();

        DateTimeFormatter fmtDate = DateTimeFormat.forPattern("dd/MM/yyyy");
        DateTimeFormatter fmtHour = DateTimeFormat.forPattern("HH:mm:ss");

        System.out.println("Date: " + fmtDate.print(processingDate));
        System.out.println("Time: " + fmtHour.withZone(DateTimeZone.UTC).
                           print(processingDate));
        System.out.println("Local date: " + fmtDate.print(localDateTime));
        System.out.println("Local time: " + fmtHour.print(localDateTime));
    }
}

出力:

Date: 06/12/2012
Time: 13:19:35
Local date: 06/12/2012
Local time: 05:19:35

問題は、デフォルトのタイム ゾーンUTC であることだけではありませんか?

于 2012-12-06T13:20:25.677 に答える