将TimeZone迁移到DateTimeZone

Che*_*eng 9 java jodatime

我正在试验Joda的时间.

    final String string_from_3rd_party = "GMT+08:00";
    // Works for standard Java TimeZone!
    System.out.println(TimeZone.getTimeZone(string_from_3rd_party));
    // Exception in thread "main" java.lang.IllegalArgumentException: The datetime zone id is not recognised: GMT+08:00
    System.out.println(DateTimeZone.forID(string_from_3rd_party));
Run Code Online (Sandbox Code Playgroud)

我如何保留string_from_3rd_party,但能够从中构建一个Joda DateTimeZone

小智 22

您可以使用Java TimeZone创建DateTimeZone:

TimeZone timeZone = TimeZone.getTimeZone(string_from_3rd_party);
DateTimeZone dateTimeZone = DateTimeZone.forTimeZone(timeZone);
Run Code Online (Sandbox Code Playgroud)