2014-10-29 64 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;