gpt4 book ai didi

java - 将 UTC 时区转换为 EST 时区

转载 作者:行者123 更新时间:2023-12-02 10:02:58 24 4
gpt4 key购买 nike

我必须将 UTC 时间戳数据转换为 EST 时区。当时区差异为 -5Hrs 时,下面的代码工作正常,但是当我给 UTC 时间时 - 2018-04-15T21:27:31.000Z那么它输出为 2018-04-15 16:27:31 -0500 这是不正确的。输出应为 2018-04-15 17:27:31 -0400。它总是减去 -5hrs

DateFormat utcFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.000'Z'");
utcFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
DateFormat estFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
estFormat.setTimeZone(TimeZone.getTimeZone("EST"));

try {
String date1 = estFormat.format(utcFormat.parse("2018-04-15T21:27:31.000Z"));

System.out.println("est time : "+date1);

} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

最佳答案

您似乎期望名为“EST”的时区简写遵守夏令时更改规则。事实并非如此。 “EST”是一年中任何时间为 GMT-5 的时区名称。

要获得符合预期的遵守夏令时规则的时区定义,最好使用使用这些时区的主要城市的名称。

根据您的情况,请尝试“America/New_York”

关于java - 将 UTC 时区转换为 EST 时区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55498080/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com