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
John BrodrickJohn Brodrick 

Bug in Datetime.newInstance(long)?

Hello All-

Maybe I'm doing something wrong here, but I am not getting the expected result.  I am trying to convert a UNIX timestamp to a DateTime object in Salesforce and the times that I am getting in the DateTime objects are way off.  Here are 3 examples:

System.debug(Datetime.newInstance(1426982400L).format());
       Debug Log: 1/17/1970 4:23 AM
       Expected: Sun, 22 Mar 2015 00:00:00 GMT

System.debug(Datetime.newInstance(1490140800L).format());
       Debug Log: USER_DEBUG|[197]|DEBUG|1/17/1970 9:55 PM
       Expected: Wed, 22 Mar 2017 00:00:00 GMT

System.debug(Datetime.newInstance(4645814400L).format());
       Debug Log: USER_DEBUG|[198]|DEBUG|2/23/1970 10:30 AM
       Expected: Mon, 22 Mar 2117 00:00:00 GMT

Again maybe there is something I am missing here.

Thanks,

John
Best Answer chosen by John Brodrick
Vla ChaVla Cha
Hi John, I think this can help you: http://www.codetalks.in/2013/08/salesforce-apex-unix-time-stamp-datetime.html

Cheers!
Vladimir

All Answers

Vla ChaVla Cha
Hi John, I think this can help you: http://www.codetalks.in/2013/08/salesforce-apex-unix-time-stamp-datetime.html

Cheers!
Vladimir
This was selected as the best answer
John BrodrickJohn Brodrick
Thanks that worked.