[SalesForce] How to correctly convert Unix Timestamp with timezone offset into Apex Datetime

There is a webservice which I'm trying to integrate in Salesforce. The webservice returns a date time in format like this

/Date(1569301200000-0500)/

I believe this is Unix Timestamp, and we can convert it into Apex Datetime using Datetime.newInstance( Long.valueOf( '1569301200000' ) )

But I'm missing the extra -0500 also in here and I'm not able to compensate for this. Can someone guide me how to accurately read the datetime in APEX with this format?

Best Answer

Refer to this document

The Unix epoch and timestamps

A JavaScript date is fundamentally specified as the number of milliseconds that have elapsed since midnight on January 1, 1970, UTC. This date and time is called the Unix epoch, which is the predominant base value for computer-recorded date and time values.

Note: It's important to keep in mind that the date and time is stored in the local time zone, and that the basic methods to fetch the date and time or its components all work in the local time zone as well.

You can use this online datetime converter to check the date and time.

1569301200000-0500 is not Unix epoch but only 1569301200000 is, and it converts to below:

enter image description here

Also Datetime.newInstance( Long.valueOf( '1569301200000' ) ) returns 2019-09-24 05:00:00

Perhaps 0500 is just indicating that its 5am in GMT or else -0500 could be time correction to get local time for which you can use DateTime.addHours or DateTime.addMinutes. In any case you need to get clarification from the webservice developers regarding what is 0500

Related Topic