2010-10-30 117 views
7

我正在試驗Joda時間。將時區移植到DateTimeZone

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,但是,能夠從它構建一個喬達DateTimeZone了呢?

回答

19

您可以使用Java的時區創建DateTimeZone:

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

這是沒有考慮到夏令時,如果你有夏令一個TimeZone配置 – Woody 2017-04-06 21:41:59