Tag Archive for utc

convert from one timezone to another

/*
* Converts time from sourceTZ TimeZone to destTZ TimeZone.
*
* @return converted time, or the original time, in case the datetime could not be parsed
*
*/
private String convTimeZone(String time, String sourceTZ, String destTZ)
{
final String DATE_TIME_FORMAT = "yyyyMMdd-HH:mm:ss";

SimpleDateFormat sdf = new SimpleDateFormat(DATE_TIME_FORMAT);

Date specifiedTime;
try {
if (sourceTZ != null)
sdf.setTimeZone(TimeZone.getTimeZone(sourceTZ));
else
sdf.setTimeZone(TimeZone.getDefault()); // default to server's timezone
specifiedTime = sdf.parse(time);
}
catch (Exception e1) {
try {
specifiedTime = new Time(time).getAsDate();
} catch (Exception e2) {
//
return time;
}
}

// switch timezone
if (destTZ != null)
sdf.setTimeZone(TimeZone.getTimeZone(destTZ));
else
sdf.setTimeZone(TimeZone.getDefault()); // default to server's timezone

return sdf.format(specifiedTime);
}

source