JSON.deserialize 添加时间

JSON.deserialize adding time

    CalEvent calEvent = (CalEvent) JSON.deserialize(calEventJson,  Technician_Dispatch_Util.CalEvent.class);
    System.debug('OGDEBUG TDU calEventJson '+calEventJson);
    System.debug('OGDEBUG TDU updateTechnicianCalEvent calEvent.startTime '+ calEvent.startTime + ' calEvent.endTime '+calEvent.endTime);

15:13:56.110 (110381617)|USER_DEBUG|[185]|DEBUG|OGDEBUG TDU calEventJson {"id":zzzzzzzzzzz","title":"zzzzz New College of zzz","allDay":false,"startTime":"2015-07-27T00:00:00.000","endTime":"2015-07-27T01:00:00.000","ownerId":"zzzzzzzzzz","description":"Perform PM visit as detailed on supplied spreadsheet. Check service office's documentation for equipment list. Contact dispatcher at Help Desk with any questions. For damaged equipment please specify the following:\n\n* Room Name:\n* Manufacturer:\n* Model:\n* Serial No.:\n* Problem: \n\nROOMS TO COVER:\n\nNew College of zz- Sarasota Fl - Carriage House\n\nNew College of zz- Sarasota zz- CHL 0\n\nNew College of zz- Fl - CHL 2z1"}

15:13:56.110 (110791126)|USER_DEBUG|[186]|DEBUG|OGDEBUG TDU updateTechnicianCalEvent calEvent.startTime 2015-07-27 04:00:00 calEvent.endTime 2015-07-27 05:00:00

从上面的日志输出可以看出,传入的json是正确的:

"startTime":"2015-07-27T00:00:00.000"

"endTime":"2015-07-27T01:00:00.000"

但是在调用 JSON.deserialize 之后,他们的时间增加了(4 小时)

calEvent.startTime 2015-07-27 04:00:00

calEvent.endTime 2015-07-27 05:00:00

为什么会这样,我该如何预防?

DateTime supports the presentation of the Date/Time in both GMT and in the time zone of the User. Is the User you are testing under in a time zone that is 4 hours different from GMT? Your debug output is I think being presented in the timezone of your User rather than in the GMT timezone.

If you change your debug output to use calEvent.startTime.timeGmt() and calEvent.endTime.timeGmt() I think you will see that the values from the JSON are preserved.

引用自 salesforce stackexchange

https://salesforce.stackexchange.com/a/88739/24064