2

我在使用 Joda Time 库获取本地时间时遇到问题。这是我正在尝试做的一个简单示例。

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));

    }

}

我期待在打印本地时间时,我会从我的应用程序运行的时区获得时间,但我得到的时间与 UTC DateTime 相同。

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 回答