从DateTime对象获取LocalDateTime

时间:2012-12-06 13:04:30

标签: java timezone jodatime

我在使用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

我在这里错过了什么?

2 个答案:

答案 0 :(得分: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吗?

答案 1 :(得分: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