function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Owen GerigOwen Gerig 

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":"a2M40000000RQeLEAW","title":"WO-00018602 New College of Florida","allDay":false,"startTime":"2015-07-27T00:00:00.000","endTime":"2015-07-27T01:00:00.000","ownerId":"00540000002kHeXAAU","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 Florida - Sarasota Fl - Carriage House\n\nNew College of Florida - Sarasota Fl - CHL 214\n\nNew College of Florida - Sarasota Fl - CHL 221"}
15:13:56.110 (110393767)|SYSTEM_METHOD_EXIT|[185]|System.debug(ANY)
15:13:56.110 (110398648)|STATEMENT_EXECUTE|[186]
15:13:56.110 (110405457)|HEAP_ALLOCATE|[186]|Bytes:56
15:13:56.110 (110440565)|SYSTEM_METHOD_ENTRY|[186]|Technician_Dispatch_Util.CalEvent.__sfdc_startTime()
15:13:56.110 (110524168)|VARIABLE_ASSIGNMENT|[-1]|this|{"allDay":false,"description":"Perform PM visit as  (429 more) ...","endTime":"2015-07-27T05:00:00.000Z","id":"a2M40000000RQeLEAW","ownerId":"00540000002kHeXAAU","startTime":"2015-07-27T04:00:00.000Z","title":"WO-00018602 New Coll (14 more) ..."}|0x7f615512
15:13:56.110 (110541459)|SYSTEM_METHOD_EXIT|[186]|Technician_Dispatch_Util.CalEvent.__sfdc_startTime()
15:13:56.110 (110580245)|SYSTEM_METHOD_ENTRY|[186]|String.valueOf(Object)
15:13:56.110 (110604337)|HEAP_ALLOCATE|[186]|Bytes:19
15:13:56.110 (110618624)|SYSTEM_METHOD_EXIT|[186]|String.valueOf(Object)
15:13:56.110 (110625066)|HEAP_ALLOCATE|[186]|Bytes:18
15:13:56.110 (110637197)|SYSTEM_METHOD_ENTRY|[186]|Technician_Dispatch_Util.CalEvent.__sfdc_endTime()
15:13:56.110 (110699785)|VARIABLE_ASSIGNMENT|[-1]|this|{"allDay":false,"description":"Perform PM visit as  (429 more) ...","endTime":"2015-07-27T05:00:00.000Z","id":"a2M40000000RQeLEAW","ownerId":"00540000002kHeXAAU","startTime":"2015-07-27T04:00:00.000Z","title":"WO-00018602 New Coll (14 more) ..."}|0x7f615512
15:13:56.110 (110715668)|SYSTEM_METHOD_EXIT|[186]|Technician_Dispatch_Util.CalEvent.__sfdc_endTime()
15:13:56.110 (110734393)|SYSTEM_METHOD_ENTRY|[186]|String.valueOf(Object)
15:13:56.110 (110745849)|HEAP_ALLOCATE|[186]|Bytes:19
15:13:56.110 (110758709)|SYSTEM_METHOD_EXIT|[186]|String.valueOf(Object)
15:13:56.110 (110774872)|HEAP_ALLOCATE|[186]|Bytes:112
15:13:56.110 (110785310)|SYSTEM_METHOD_ENTRY|[186]|System.debug(ANY)
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
15:13:56.110 (110796707)|SYSTEM_METHOD_EXIT|[186]|System.debug(ANY)

You can see from the above log output that the json being passed in are correct:
"startTime":"2015-07-27T00:00:00.000"
"endTime":"2015-07-27T01:00:00.000"
However after calling JSON.deserialize time is added onto them (4 hours)
calEvent.startTime 2015-07-27 04:00:00
calEvent.endTime 2015-07-27 05:00:00

Why is this and how can I prevent against this?
 
Best Answer chosen by Owen Gerig
Boris BachovskiBoris Bachovski
By default the date and datetime variables will be converted into the current user's timezone. You need to explicitly debug the date or datetime variable and use the *GMT methods in order to get the correct time. The methods can be found here - https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_datetime.htm

Also note in the JSON, the format of your datetime is in GMT format.

All Answers

Boris BachovskiBoris Bachovski
By default the date and datetime variables will be converted into the current user's timezone. You need to explicitly debug the date or datetime variable and use the *GMT methods in order to get the correct time. The methods can be found here - https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_datetime.htm

Also note in the JSON, the format of your datetime is in GMT format.
This was selected as the best answer
Owen GerigOwen Gerig
How can I get a DateTime object that when serialized to json will show the DateTime in gmt?