Tuesday, September 11, 2012

Offset for given Time Zone

import java.util.TimeZone;

public String getTimeZoneOffSet(String timeZoneID) {
        TimeZone tz = TimeZone.getTimeZone(timeZoneID);
        int rawOffset = tz.getRawOffset();
        int hour = rawOffset / (60 * 60 * 1000);
        int minute = Math.abs(rawOffset / (60 * 1000)) % 60;
        return hour + ":" + minute;
    }

Test:
 tc.getTimeZoneOffSet("America/New_York")); This returns Offset for NY -->-5:0

Using Calendar Class

import java.util.Calendar;

public int getOffsetForTimeZone(String timeZoneId) {
        int a;
        Calendar calendar = new GregorianCalendar();
        TimeZone timeZ = calendar.getTimeZone();
        timeZ = timeZ.getTimeZone(timeZoneId);
        a = (int) ((timeZ.getRawOffset()) * (2.77777778 / 10000000));
        return a;
    }