简体   繁体   中英

Convert UTC to Eastern Prevailing Time (EDT or EST)

有人可以告诉我如何将JodaTime中的UTC日期转换为东部普遍时间(取决于一年中的时间是EST还是EDT?)

使用withZone()可将Joda-Time对象转换为其他时区。

Joda takes care of it by itself. There is a slight difference between using the TimeZone and the Locale of a place.

Take a look at the following examples.

Using Locale:

Using @BillMan's suggestion setting DateTimeZone to a locale will actually change the time zone.

System.out.println("Winter " + DateTime.now(DateTimeZone.UTC).parse("2015-12-01T12:00:00").toDateTime(DateTimeZone.forID("America/New_York")).toString());
System.out.println("Summer " + DateTime.now(DateTimeZone.UTC).parse("2015-05-01T12:00:00").toDateTime(DateTimeZone.forID("America/New_York")).toString());

Returned:

Winter 2015-12-01T12:00:00.000-05:00
Summer 2015-05-01T12:00:00.000-04:00

Using TimeZone:

Note that both times are set to 12:00:00, and the result in Summer got moved by an hour, (but the offset stayed the same)

System.out.println("Winter " + DateTime.now(DateTimeZone.UTC).parse("2015-12-01T12:00:00").toDateTime(DateTimeZone.forID("EST")).toString());
System.out.println("Summer " + DateTime.now(DateTimeZone.UTC).parse("2015-05-01T12:00:00").toDateTime(DateTimeZone.forID("EST")).toString());

Returned:

 Winter 2015-12-01T12:00:00.000-05:00
 Summer 2015-05-01T11:00:00.000-05:00

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM