user2874190 user2874190 - 6 months ago 19
Android Question

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

I am writing an app about GPS and I have to convert the string of UTC time obtained from NMEA format to local time.

The strings are formatted as "193526" which represents 19:35:26 UTC(GMT).

How can I convert it to local time such as "15:35:26 EDT"?

Answer

EDT TimeZone does not exist in jdk. See the answer here.

You can do the conversion this way:

    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

Comments