[SalesForce] Why is the date saving as an hour later

The company default time zone is GMT (+00:00), and a date sent using the API is coming through like this:

09:16:37.032 (32355740)|USER_DEBUG|[17]|DEBUG|Date/Time: 2014-06-19T10:16:00.977

It arrives as a string in an HTTP GET request, the following apex code runs to convert it to the only datetime format it seems to accept:

relatedRecord.Receipt_Received_At__c = Datetime.valueOf(g_datetime.substring(0, 19).replace('T', ' '));
update relatedRecord;

But then when I go to view it in the saved record detail page, it is showing 11:16, not 10:16. I've tried changing valueOf() to valueOfGmt() but it doesn't change it. Why is the date saving as GMT+01:00?

Best Answer

I believe the issue is that your date is being sent through without knowledge of the timezone it is working in and so is assuming GMT and then displaying it as BST (correctly given we are in BST currently). Salesforce stores all times in GMT and then using your locale determines the correct display and return values.

Try adding a timezone designator (such as +01:00) to your string to get the system to recognise it is being sent in as BST and so should save internally as GMT (1 hour behind). The system generating your string will need to handle this going forward though by correctly parameterizing the datetimes for use in the future.