How to convert UTC to IST conversion in apex?

2.9K    Asked by ChloeBurgess in Cyber Security , Asked on Jul 21, 2021

I am getting the time as string like "client_modified": "2020-02-04T11:27:34Z" How can I convert this to local time zone,

Update:

I am getting this time from api response I need to display it in lightning:formatteddatetime, Sort those records based on date time so am doing this conversion in controller side

code:

string s = string.valueof(a2.get('server_modified')); DateTime LDate = null; if(s != null && s != ''){ s = s.replace('T', ' '); s = s.replace('Z', ''); LDate = DateTime.valueof(s); }

 How to convert utc to ist in apex?

Answered by Brian Kennedy

Salesforce store the time in UTC but when we use apex:outputfield in VF page, Salesforce convert the datetime to based on user locale so you don't need to do anything.

If you still have any other use case then I suggest use below code to convert

Use Date.format() method and pass the time Zone.It will convert utc to ist for you.

//Get current date time in GMT format Datetime dt = System.now(); //Convert it into Indian Standard Time(dtIST). String dtIST = dt.format('yyyy-MM-dd HH:mm:ss', 'IST'); System.debug('dt-' + dt); System.debug('dtIST-' + dtIST);



Your Answer

Interviews

Parent Categories