2014-10-29 69 views
1

我將Joda Time加入了我的Android項目,以便能夠使用更新的時區信息。爲什麼Joda Time報告時區錯誤的偏移量,而Android Java Api返回正確的偏移量?

不幸的是,我收到了孟加拉國用戶的報告,發現時區偏移量計算錯誤。

在模擬器上測試它,我也得到不正確的偏移量。使用標誌-timezone Asia/Dhaka我看到這在我的應用程序運行的Android模擬器後:使用普通的Java API

TimeZone zone = TimeZone.getDefault(); 
String zoneName = zone.getDisplayName(daylight, TimeZone.SHORT); 
int millisOffset = zone.getOffset(System.currentTimeMillis()); 
=> Asia/Dhaka, Offset +6 hours 

使用約達時間庫

DateTime today = new DateTime(); 
String name = today.getZone(); 
int millisOffset = today.getZone().getOffset(today.getMillis()); 
=> Asia/Dhaka, Offset +7 hours 

報道

報道區偏移量是不是有什麼我丟了?

+0

您正在使用什麼版本的喬達的喬達時間的最新版本? – 2014-10-29 19:52:13

+0

@MattBall 2.3.3,據我所知。我還在一週前更新了tzdata。 – you786 2014-10-29 19:53:34

+0

我無法重現該問題。 [這段代碼](https://gist.github.com/mjball/e634b1ab1956c1c87bda)打印'21600000',這是6小時,因此是正確的。 – 2014-10-29 19:56:49

回答

0

爲了讓喬達給出正確的偏移量,您必須提供一個日期時間instant.With無日期時間瞬間,因爲我們有不同的偏移量(夏時制)不可能計算偏移量。這就是我將如何使用Joda來抵消+ HH:mm格式:

int offsetInMillis = DateTimeZone.forID(zoneId).getOffset(new DateTime().getMillis()); 
    String offset = String.format("%02d:%02d", Math.abs(offsetInMillis/3600000), 
      Math.abs((offsetInMillis/60000) % 60)); 
    offset = (offsetInMillis >= 0 ? "+" : "-") + offset;