有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java将UTC转换为EST时区

我必须将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();
}

共 (0) 个答案