如何将包含UTC时间的字符串(如“193526”(19:35:26))转换为本地时区?

时间:2015-03-26 20:26:34

标签: java android datetime-format

我正在编写一个关于GPS的应用程序,我必须将从NMEA格式获得的UTC时间字符串转换为当地时间。

字符串的格式为" 193526"代表格林威治标准时间19:35:26。( 如何将其转换为当地时间,例如" 15:35:26 EDT"?

1 个答案:

答案 0 :(得分:0)

在jdk中不存在EDT TimeZone。请参阅the answer here

您可以通过这种方式进行转换:

    SimpleDateFormat formatUTC = new SimpleDateFormat("HHmmss");
    formatUTC.setTimeZone(TimeZone.getTimeZone("UTC"));
    Date date = formatUTC.parse("193526");

    SimpleDateFormat formatEDT = new SimpleDateFormat("HH:mm:ss z");
    formatEDT.setTimeZone(TimeZone.getTimeZone("GMT-04:00"));
    System.out.println(formatEDT.format(date));
  

15:35:26 GMT-04:00