joda - 如何将utc中的datetime格式化为本地时间格式

时间:2015-09-03 00:49:25

标签: java datetime jodatime

我有一个日期时间对象,其值为格式(通过断点)

dateTime = 2015-09-04T08:37:00.440-07:00

我正在尝试获得相应的当地时间

我使用了格式"EEEE, MMM d 'at' hh:mm a"

dateTime.toString(format);

但是它不需要-07:00小时抵消并给予回报:

Friday, Sep 4 at 08:37 AM

预期价值:Friday, Sep 4 at 01:37 AM

我错过了什么?

1 个答案:

答案 0 :(得分:1)

您要求通过调用withZone(DateTimeZone.getDefault())来转换为本地(默认)时区。

DateTime dateTime = DateTime.parse("2015-09-04T08:37:00.440-07:00");
System.out.println(dateTime);
System.out.println(dateTime.toString("EEEE, MMM d 'at' hh:mm a"));

dateTime = dateTime.withZone(DateTimeZone.getDefault());
System.out.println(dateTime);
System.out.println(dateTime.toString("EEEE, MMM d 'at' hh:mm a"));

dateTime = dateTime.withZone(DateTimeZone.UTC);
System.out.println(dateTime);
System.out.println(dateTime.toString("EEEE, MMM d 'at' hh:mm a"));

输出

2015-09-04T08:37:00.440-07:00
Friday, Sep 4 at 08:37 AM

2015-09-04T11:37:00.440-04:00
Friday, Sep 4 at 11:37 AM

2015-09-04T15:37:00.440Z
Friday, Sep 4 at 03:37 PM