I'm trying to take a string in the format of "HH:mm" and convert it from local to utc and vice versa. Here's what I have so far:
private String doTimeConversion(String time, boolean type) {
DateFormat dfLocal = new SimpleDateFormat("HH:mm");
DateFormat dfUTC = new SimpleDateFormat("HH:mm");
TimeZone local = TimeZone.getDefault();
TimeZone utc = TimeZone.getTimeZone("UTC");
dfLocal.setTimeZone(local);
dfUTC.setTimeZone(utc);
Date date;
String newTime = "00:00";
try {
//If true: local to utc/If false: utc to local
if (type) {
date = dfLocal.parse(time);
newTime = dfUTC.format(date);
} else {
date = dfUTC.parse(time);
newTime = dfLocal.format(date);
}
} catch (ParseException e) {
e.printStackTrace();
}
return newTime;
}
This seems to work mostly but the issue I'm having is that, although I'm supposed to be grabbing the devices time with TimeZone.getDefault() it is not taking into account Daylight Savings. Instead of changing 12:00(UTC) to 08:00(EDT) it changes it to 07:00(EST) instead.
Overall I just want to be able to take the time that the user has entered (the string) and use the devices time zone to assume what the user has entered is according to their timezone and convert it to utc as well as from utc back to the devices timezone.