I want to parse a time value formatted as "12:34:56", which comes from an external web service, to a Date, Calendar or anything that I can later compare to current time.
The problem is that on Android, DateFormat.parse seems to ignore the user’s default timezone and parse the value as if it was in UTC. Given this code:
DateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
String inTime = "12:34:56";
Date time = timeFormat.parse(inTime);
Date now = new Date();
I get the wanted result on my desktop (Java 1.6.0_29):
Thu Jan 01 12:34:56 CET 1970
Mon Nov 21 20:53:04 CET 2011
In other words, the string “12:34:56” is parsed in the default timezone. However, on Android this seems to be parsed in UTC, because it is one hour off:
Thu Jan 01 11:34:56 GMT+0100 1970
Mon Nov 21 20:53:02 GMT+0100 2011
I even tried setting the timezone using timeFormat.setTimeZone(TimeZone.getDefault()), but this does not help. Is there any way to force DateFormat.parse to parse the time in the default (user) timezone on Android, or do I have to convert everything to UTC and back to get it working?
It turns out my device really had some issues with time-zones. I tried running the code on some other devices and it worked as expected.
I resolved the issue for my phone by installing TimeZone Fixer and updating the timezone information.
I will leave my code as-is, but I will have to implement some workarounds if the problem turns out to be more common.