【问题标题】:SimpleDateFormat adds 1 hour when convert UTC time to Australia time zone [duplicate]将 UTC 时间转换为澳大利亚时区时,SimpleDateFormat 增加 1 小时 [重复]
【发布时间】:2016-09-15 07:23:47
【问题描述】:

我使用以下代码将 UTC 时间转换为设备当前时区时间。它在印度标准时间 (IST) 上运行良好。但是当设备时区是澳大利亚悉尼时,它会额外增加 1 小时。

例如,我给 UTC 时间,例如 2016-10-10T09:10:00,它转换为晚上 8:10,但实际上我需要晚上 7:10。我的代码有什么问题?

public static String convertToLocalTimeFormat(String utcDateTimeString) {
    SimpleDateFormat simpleDateFormat;
    String formattedDateString = "";

    simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.getDefault());

    simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    Date parsedDateObject;
    try {
        parsedDateObject = simpleDateFormat.parse(utcDateTimeString);
        SimpleDateFormat expectedSimpleDateFormat = new SimpleDateFormat("hh:mm a", Locale.getDefault());
        formattedDateString = expectedSimpleDateFormat.format(parsedDateObject);
    } catch (ParseException e) {
        e.printStackTrace();
        return formattedDateString;
    }
    return formattedDateString;
}

【问题讨论】:

  • 夏令时?
  • @Tibrogargan 是的...目前在澳大利亚是澳大利亚东部标准时间,但是给定的日期是 10 月...所以在 10 月 1 日之后是澳大利亚东部夏令时间。所以它增加了1小时。但我需要同时显示我现在输入的内容。

标签: java android android-date


【解决方案1】:

我建议尝试

System.out.println(expectedSimpleDateFormat.getTimeZone());

查看该设备上的所有夏令时方案是否正确。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-25
    • 2015-07-29
    • 2022-01-05
    • 1970-01-01
    • 2021-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多