I converted the local time to GMT / UTC and vice versa using these two methods and it works fine without any problems for me.
public static Date localToGMT() {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
Date gmt = new Date(sdf.format(date));
return gmt;
}
pass the GMT / UTC date that you want to convert to the local device time for this method:
public static Date gmttoLocalDate(Date date) {
String timeZone = Calendar.getInstance().getTimeZone().getID();
Date local = new Date(date.getTime() + TimeZone.getTimeZone(timeZone).getOffset(date.getTime()));
return local
}
source
share