How to convert Unix Timestamp to DateTime using Apex in Salesforce?

How to convert Unix Timestamp to DateTime using Apex in Salesforce?

Sample Code:
DateTime currentDT = System.now();
String strUnixTimeStamp = String.valueof( currentDT.getTime() );
System.debug( ‘String UnixTimeStamp is ‘ + strUnixTimeStamp );
Long longUnixTimeStamp = Long.valueOf( strUnixTimeStamp );
System.debug ( DateTime.newInstance( longUnixTimeStamp ) );

Output:

Note: It returns the value in GMT/UTC.

Leave a Reply