代码之家  ›  专栏  ›  技术社区  ›  Cheok Yan Cheng

将时区迁移到日期时区

  •  9
  • Cheok Yan Cheng  · 技术社区  · 15 年前

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

    我怎样才能保留 string_from_3rd_party ,但是,能够构建一个Joda DateTimeZone 从里面出来?

    1 回复  |  直到 15 年前
        1
  •  22
  •   Ventral    15 年前

    您可以使用Java时区创建日期时区:

    TimeZone timeZone = TimeZone.getTimeZone(string_from_3rd_party);
    DateTimeZone dateTimeZone = DateTimeZone.forTimeZone(timeZone);