function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
PedroLPedroL 

Creating a datetime instance using a specific time zone

Hello,

 

I need to create records to check the SLA on our daily batch operations. The SLAs times are stored in its own object using a string to indicate HH:MM and a picklist value to specify the timezone for the client. Each day I need to generate the expected SLA record based on the date and the time stored in SLA times object to compare against the actual. Ideally, I should be able to create a datetime instance specifying the relevant time zone. However, the only options avaialble in Apex are GMT or current user locale.

 

Is there a way to accomplish the same using what's available in Apex?

 

I could store the time offset from our default time zone (EST) in the SLA object. However, I would need to deal with the periods when Europe and North America switch daylights savings at different dates.

 

Thanks,

 

Pedro

Best Answer chosen by Admin (Salesforce Developers) 
Ken KoellnerKen Koellner

You probably want to use the values in User.TimeZoneSidKey like (America/New_York) to represent time zones as they   are standard.  I found that I could construct a date in GMT and a specific time zone.  Compute the difference, then adjust the input date.  That's inelegant but it works. 

 

        string dateTimeStr = inDateTime.format('yyyy-MM-dd HH:mm:ss',  timeZoneStr);
        string dateGmtStr  = inDateTime.formatGMT('yyyy-MM-dd HH:mm:ss');
        Datetime localDateTime = DateTime.valueOf(dateTimeStr);
        Datetime baseGMTTime = DateTime.valueOf(dateGMTStr);
        Long milliSecDiff =  baseGMTTime.getTime() - localDateTime.getTime();
        Long minDiff = milliSecDiff / 1000 / 60;
        Datetime outDateTime = inDateTime.addMinutes(minDiff.intValue());

All Answers

Ken KoellnerKen Koellner

You probably want to use the values in User.TimeZoneSidKey like (America/New_York) to represent time zones as they   are standard.  I found that I could construct a date in GMT and a specific time zone.  Compute the difference, then adjust the input date.  That's inelegant but it works. 

 

        string dateTimeStr = inDateTime.format('yyyy-MM-dd HH:mm:ss',  timeZoneStr);
        string dateGmtStr  = inDateTime.formatGMT('yyyy-MM-dd HH:mm:ss');
        Datetime localDateTime = DateTime.valueOf(dateTimeStr);
        Datetime baseGMTTime = DateTime.valueOf(dateGMTStr);
        Long milliSecDiff =  baseGMTTime.getTime() - localDateTime.getTime();
        Long minDiff = milliSecDiff / 1000 / 60;
        Datetime outDateTime = inDateTime.addMinutes(minDiff.intValue());

This was selected as the best answer
PedroLPedroL

Ken,

 

Thanks a lot. I use your sample to compute the difference between GMT and the required time zone for a particular date and then adjust the datetime variable accordingly. Since we are lacking the Apex functionality to directly do this, I'm not sure you could do this in a simpler more elegant way.

 

Thanks,

 

Pedro

Paul Juneau 4Paul Juneau 4
I don't like using the diff function because when you peform the addMinutes operation, you have to know to add or subtract based on the desired timezone's location with respect to the GMT time zone. I instantiate a datetime via JSON deserialization instead.
 
Datetime dtNow = Datetime.now();
String dtNowAsStringGMT = String.valueOfGMT(dtNow);
String dtNowAsStringInLocalTZ = dtNow.format('yyyy-MM-dd\'T\'HH:mm:ss',Userinfo.getTimeZone().getID());
dtNowAsStringInLocalTZ += '.000Z'; 
dtNowAsStringInLocalTZ = '"'+dtNowAsStringInLocalTZ+'"';
System.debug('dtNowAsStringGMT: '+dtNowAsStringGMT);
System.debug('dtNowAsStringInLocalTZ: '+dtNowAsStringInLocalTZ);

Datetime dtNowInLocalTZ = (Datetime)JSON.deserialize(dtNowAsStringInLocalTZ, Datetime.class);
system.debug(dtNowInLocalTZ);