我在 Android 中创建一个微调器,然后获取微调器的值并尝试计算时区 ID 中的本地时间。
在以下示例中,timeZoneID 是时区 ID 的字符串表示形式,例如“美国/芝加哥”等
DateTimeZone tz = DateTimeZone.forID(timeZoneID);
DateTime now = DateTime.now(DateTimeZone.forID(timeZoneID));
DateTime localTime1 = now.toDateTime(tz);
float minuteRotation = localTime1.getMinuteOfDay() / 30f * (float) Math.PI;
float hourRotation = ((localTime1.getHourOfDay() + localTime1.getMinuteOfDay() / 60f) / 6f) * (float) Math.PI;
然而,问题是时区的本地时间总是错误的。例如,如果 PST 时间应该是晚上 8 点,那么我从上面的代码中返回下午 5 点。
所有时区 ID 都是正确的,并且是从 Joda 时间本身获得的,这些时间填充在以下微调器中:
Set<String> zoneIds = DateTimeZone.getAvailableIDs();
DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("ZZ");
ArrayList<String> TZ1 = new ArrayList<String>();
for(String zoneId:zoneIds) {
TZ1.add("("+dateTimeFormatter.withZone(DateTimeZone.forID(zoneId)).print(0) +") "+zoneId+", "+TimeZone.getTimeZone(zoneId).getDisplayName());
}
for(int i = 0; i < TZ1.size(); i++) {
adapter.add(TZ1.get(i));
}
final Spinner TZone = (Spinner)findViewById(R.id.TimeZoneEntry);
TZone.setAdapter(adapter);
for(int i = 0; i < TZ1.size(); i++) {
if(TZ1.get(i).equals(TimeZone.getDefault().getDisplayName())) {
TZone.setSelection(i);
}
}
问题:为什么我从 Joda 时间得到的本地时间不正确?我无能为力试图解决这个问题!
最佳答案
以下代码作为测试报告了我的时区设置的正确本地时间:
@Test
public void timeTest()
{
String timeZoneID = "Europe/Berlin";
DateTimeZone tz = DateTimeZone.forID(timeZoneID);
DateTime now = DateTime.now(DateTimeZone.forID(timeZoneID));
DateTime localTime1 = now.toDateTime(tz);
System.err.println(localTime1 + " " + localTime1.getZone().getID() + " Default: " + TimeZone.getDefault().getID());
}
当前的设置不应该是 PDT
而不是 PST
吗? “美国/芝加哥”比太平洋夏令时间晚 3 小时。
关于java - Joda Time 给出了不正确的时区时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31678851/