java - Joda Time 给出了不正确的时区时间

标签 java android time jodatime wear-os

我在 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/

相关文章:

java - Java中如何初始化日期类型的变量?

java - datainputstream 接收消息时抛出 eofException

android - android logcat中/dev/log/main和/dev/log/system的意义是什么?

java - 通用 EventManager - 在 3 行代码 :-) 后卡住

linux - 将 bash 脚本中的时间与 ± x 分钟进行比较

javascript - 检查时间是否在javascript中的小时和分钟两个值之间

clock_gettime 返回一些非常大的值

java - 如何在 WEB-INF 文件夹中使用带有 JAR 的 taglibs JSTL/core

java - 预期的空指针导致意外的 InvokingTargetException

android - 在 React Native 中的两个独立应用程序之间共享代码