1

We have incoming JSON from one of our client using REST API. The JSON they send has a timestamp value as a String like this "timestamp": "2017-01-11 19:23:27 CST".

CST here being China Standard Time not Central Standard Time zone of US.

I am trying to map that value to a DateTime custom field in a custom sObject and do an insert.

Timestamp is String which I am converting to DateTime by using

Datetime myDate = Datetime.valueOf(timestamp);

Using the above function the myDate value is getting converted to GMT.

Is there any way that I can use to just convert timestamp string value into DateTime value without GMT conversion and insert into custom object as it is?

The timestamp needs to have the value unchanged from JSON because of some reporting requirements.

  • That's not even a standard "date" format, for the very reason you've observed. I'd suggest you tell your service provider/developers/etc to use, at minimum, GMT, or if that's not acceptable, the -HH:MM format (e.g. 17:10:05+08:00). Even if you get this working, someone somewhere somehow is bound to break it sooner or later. – sfdcfox Jan 25 '17 at 18:04
  • I am afraid that's not possible from their end to format to standard datetime format, they are pushing tons of JSONs and are limited to do any formatting at their end. Datetime myDate = Datetime.valueOf(timestamp); String myDate2 = myDate.format('yyyy-MM-dd HH:mm:ss', 'Asia/Shanghai'); works for me but the function myDate.format returns a String which cannot be mapped to DateTime field while inserting the sObject. Using Datetime.valueOf() on myDate2 variable converts it back to GMT. – Win4frndz Jan 25 '17 at 18:14
1

Salesforce always stores dates and date/times in GMT. This is a standard practice, as it makes it a lot easier to convert those date/time values to the user's timezone settings, and requires less storage space per datetime value. So, either you want your data as a DateTime in GMT and all the report capabilities you get when you use the DateTime data type, or you want a String that can store your literal text, which will cripple your reporting capabilities. If a user wants to view reports in China Standard Time, they can go to their settings and change their timezone settings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.