
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.