Where to get UTC to IST converter in apex?

758    Asked by DavidEdmunds in Salesforce , Asked on May 9, 2023

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);
}
Answered by Dipika Agarwal

To get utc to ist converter - Salesforce stores the time in UTC but when we use apex:outputfield in the VF page, Salesforce converts the datetime to based on the 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