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

How to convert the string containing UTC time such as "193526"(19:35:26) to local time zone?

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

字符串的格式为“193526”,表示 19:35:26 UTC(GMT)。
如何将其转换为当地时间,例如“15:35:26 EDT”?

EDT TimeZone 在 jdk 中不存在。参见 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