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
aravindaaaaaravindaaaa 

Handling UTC DateTime in Apex

Hi,

 

SInce I'm new to Apex development, I'm not quite sure how to handle a UTC datetime like '2012-04-23T14:54:29+05:30' . I just wanted to display the above value as Apr 23, 2012 2:54 PM to the user.

 

I tried datetime.getInstance('2012-04-23T14:54:29+05:30') and it threw an error saying "Invalid Date/Time" . 

Any pointers on how to use the datetime class to do the above task can be helpful. Thanks. 

Best Answer chosen by Admin (Salesforce Developers) 
sai.sfsai.sf

Convert it into a proper string to transform to Salesforce datetime variable. And then use datetime.valueof().

 

In your case:

string dttm = '2011-05-16T18:30:08.923-07:00';
string dttm2 = dttm.replace('T',' ');
Integer indx = dttm2.indexOf('.');
string newstr = dttm2.substring(0,indx);
system.debug('===========Time=========  ' +newstr );
datetime objdt  = datetime.valueof(newstr );
system.debug('===========Final Time=========  ' +objdt  );

 

Hope this helps.

All Answers

sai.sfsai.sf

Convert it into a proper string to transform to Salesforce datetime variable. And then use datetime.valueof().

 

In your case:

string dttm = '2011-05-16T18:30:08.923-07:00';
string dttm2 = dttm.replace('T',' ');
Integer indx = dttm2.indexOf('.');
string newstr = dttm2.substring(0,indx);
system.debug('===========Time=========  ' +newstr );
datetime objdt  = datetime.valueof(newstr );
system.debug('===========Final Time=========  ' +objdt  );

 

Hope this helps.

This was selected as the best answer
aravindaaaaaravindaaaa

Thanks for the quick response sai.sf . Just wanted to know if removing the timezone difference from the datetime string is a good idea? If any of my salesforce users views the data from a different timezone, isn't he going to see a conflicting time? Please let me know.