1
votes

I have requirement to to store the timestamp returned from the response to Salesforce DateTime. But the timestamp returned is in Unix format. How do I convert it to DateTime in Salesforce using apex?

2

2 Answers

2
votes

This is a super old question, but I found an answer.

I was grabbing some data from a REST API that had a unix timestamp as the modified date. I needed to convert the unix timestamp into a Datetime object using Apex in Salesforce.

I found this code at: http://www.codetalks.in/2013/08/salesforce-apex-unix-time-stamp-datetime.html

Also make sure that the utcTime variable is cast as an Integer as Datetime.addSeconds() wants an Integer.

utcTime = 1376986594;
createdDate = Datetime.newInstanceGmt(1970, 1, 1, 0, 0, 0);
createdDate.addSeconds(utcTime);
System.debug('DateTime: ' + createdDate);
0
votes

Try integer.valueOf instead of long.valueOf