I want to save event in picked timezone. So the cese is: If user location Moscow, he want to create meeting in London, he checks picklist London. And the event must be saved in London time. For example He picks London and time 20:00. After save it will be 22:00 by Moscow standard time. How can it be accomplished?

I tried it in different ways:

  1. Through date parse - no results
  2. By changing current user locale - from time to time it works

This is the save button:

public PageReference save() {

    //Custom validation errors
        ChangeTimeZone(); // here I try to set the the local timezone to get current value of datetime                            
        //creating the event after there are no error messages                   
        Event ev = (Event)stdcontroller.getRecord();      
        **ev.ActivityDateTime = m_repository.parse(startDate, startHour); // Here I try to parse datetime to get the local time.
        insert ev;                                

        //Save standard fields         
        ChangeTimeZoneBack(); //here I try to set back current user timezone

        //return page reference            
        String explicitReturnUrl = ApexPages.currentPage().getParameters().get('returnurl');
        String returnUrl = (ev.Id != null) 
            ? String.format('{0}/{1}', new String[] { Url.getSalesforceBaseUrl().toExternalForm(), ev.Id }) 
            : ((explicitReturnUrl != null) ? explicitReturnUrl : Url.getSalesforceBaseUrl().toExternalForm());
                PageReference page = new PageReference(returnUrl);
        return page;                      
    catch (Exception exc) {
    return null;

here the date parse code that I use:

//Parsing the Date Time for the current Time    
public Datetime parse(String startDate, String startHour) {
    List<String> hourComponents = startHour.split(':');
    return Datetime.newInstance(
            0, 0

And here the functions to update user location

public PageReference ChangeTimeZone() {             
    Event ev = (Event)stdcontroller.getRecord();
    if (ev.Activity_Location__c == 'London') {
        User us = CurrentUser;
        us.TimeZoneSidKey = 'Europe/London';
        update us;
    return null;        

public PageReference ChangeTimeZoneBack() {
    User usupdate = CurrentUser;  
    usupdate.TimeZoneSidKey = string.valueOf(UserTimeZone);
    update usupdate;
    return null;

Try doing -

  1. Create a custom setting for your countries list & Salesforce timezone id's i.e.(London -> Europe/London)
  2. Using the Timezone Id above get the timezone something like - TimeZone tz = TimeZone.getTimeZone('Europe/London');
  3. Create a Date/Time instance of the date & time selected DateTime dtpost = DateTime.newInstanceGMT(2012, 11, 1, 0, 0, 0);
  4. Based on the output from step 2 & 3 get the offset between the Date/Time field & the timezone - tz.getOffset(dtpost)
  5. Convert the offset received in Step 4 (in milliseconds) to hours.
  6. Add the hours from Step 5 to your Date/Time field in Step 3 using the time class addHours(additionalHours) method.
  • Sorry, could you explain how to convert offset to hours in step 5? – Viktor Apr 17 '17 at 12:01
  • Offset can be calculated - offset / (1000 * 60 *60). I found another good link for you where the code is also explained - [salesforce.stackexchange.com/questions/8538/… – Nikhil Khivansara Apr 17 '17 at 12:35
  • Ok, i get it. So now I get the next: When user select (e.x.) 2017-04-17 20:00:00 we get the offset of Europe/London in mileseconds (3600000 = 1 hour) and added to this datetime. We will get the next 2017-04-17 21:00:00 but it is wrong. I need to set the datetime in London standard time. So if My Locale is Moscow and i try to create event for London it must be 20:00:00 by London Time and 22:00:00 by Moscow time. – Viktor Apr 17 '17 at 13:54

So i resolve my problem. Instead of calculating datetime for TimeZone, we need to change time zone for current user and then save the datetime in his local time. So when the user pick location "London" and select the time to 20:00 we change the time zone of the user to 'Europe/London' and parse the date by local time zone. After parsing we save this value in our datetime field. After save action we set back the time zone of the user.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.