How To Convert UTC To IST Conversion In Apex?

689    Asked by AndreaBailey in Cyber Security , Asked on Nov 18, 2022
 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 Amit raj

Salesforce store the time in UTC -5 to ist 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 the time 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