Skip to main content
1 of 5
Jesse Milburn
  • 2.7k
  • 1
  • 18
  • 35

You need to calculate the offset of the time element of your Datetime object.

public static Integer timeZoneOffset_hours(Date d, String tz) {
    TimeZone tzSelected = TimeZone.getTimeZone(tz);
    TimeZone tzGmt = TimeZone.getTimeZone('Greenwich Mean Time');

    Integer gmtOffset = tzGmt.getOffset(d);
    Integer selectedOffset = tzSelected.getOffset(d);
    Integer hours = (selectedOffset - gmtOffset) / 1000 / 60 / 60; 

    return hours;
}

Using this you can pass the date portion of your datetime object and the timezone you wish to convert to.

You can easily get the timezone string of the user by:

UserInfo.getTimeZone();

Then add the offset to your datetime:

d.addHours(the offest);
Jesse Milburn
  • 2.7k
  • 1
  • 18
  • 35