How to Convert UTC Time Zone to User Time Zone in Salesforce

SOQL queries in a client application return dateTime field values as Coordinated Universal Time (UTC) values. You need to manually handle to convert this UTC time zone to your User Time zone.

For this salesforce provides, convertTimezone method. But You can use convertTimezone() in a date function.
Let see how to convert UTC time zone to User Time Zone,
  1. DateTime scheduleTime = DateTime.valueofGmt( ‘Your Date Time to be Convert’);
  2. // Get User time zone.              
  3. Timezone userTimezone = UserInfo.getTimezone();
  4. Integer userOffset = userTimezone.getOffset(scheduleTime);
  5. // Get Organization time zone.
  6. Timezone orgTimeZone = Timezone.getTimezone(‘America/Los_Angeles’);
  7. Integer orgOffset = orgTimezone.getoffset(scheduleTime);
  8. Integer offsetDifference = orgOffset  userOffset;
  9. // DateTime is converted from UTC timezone to User Timezone
  10. DateTime correctedDateTime = scheduleTime.addMinutes(offsetDifference / (1000 * 60));
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a comment