1
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

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 it?

FYI Troubleshooting further I tried something different

Map<String, Object> resultsMap = (Map<String, Object>) JSON.deserializeUntyped(calEventJson);
    String startTime = (String)resultsMap.get('startTime');//hack for issue with incorrect time saved
    String endTime = (String)resultsMap.get('endTime');

This startTime will have the correct String time But I cant convert it to Datetime as I get errors

Invalid date/time: 2015-07-28T00:00:00.000


1

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.

  • Thank you very much that does resolve the discrepancy. However I cannot assign this to my DateTime object as it strips out the date. How can I get my DateTime object to have my date and time as GMT – owen gerig Aug 19 '15 at 21:31
  • @owengerig The values are held in a DateTime as GMT and offset to the User's timezone in some of the methods and not in others. For example the getTime method is GMT based. I don't understand what you mean by "I cannot assign this to my DateTime object". – Keith C Aug 19 '15 at 21:36
  • So whether im saving them or retrieving them they are DateTime objects. But its only when i do .timeGmt() that the hours are correct. if i just read or save the StartDateTime object it reads the +4 hour time. Basically I need DateTime object with StartDateTime's date and StartDateTime's time get what is returned from timeGmt() – owen gerig Aug 19 '15 at 21:40
  • @owengerig If you really want to deal with dates not date/times then grab the GMT date using dateGmt and just work with the dates. If you use DateTime across your code, and want to stay in GMT, you will have to be careful to use the various methods ending in "Gmt" everywhere. – Keith C Aug 19 '15 at 21:44
  • no i want both date and time but i need it without the users timezone coming into play. thank you though – owen gerig Aug 19 '15 at 21:50

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.