JSON.serialize is mucking with my datetime objects. How do I fix this?

I feel that the following assertion should be true, but it is not:

DateTime testDate = DateTime.now();
System.assertEquals('"' + testDate.format('yyyy-MM-dd'T'hh:mm:ss.SSS'Z'' + '"'), JSON.serialize(testDate));

I get the following error:

16:38:28.966 (2966802000)|FATAL_ERROR|System.AssertException: Assertion Failed: Expected: “2014-04-03T04:38:27.988Z”, Actual: “2014-04-03T20:38:27.988Z”

Update: when using formatGMT I still get the error:

DateTime testDate = DateTime.now();
System.assertEquals('"' + testDate.formatGMT('yyyy-MM-dd'T'hh:mm:ss.SSS'Z'' + '"'), JSON.serialize(testDate));

16:51:32.228 (2228010000)|FATAL_ERROR|System.AssertException: Assertion Failed: Expected: “2014-04-03T08:51:31.274Z”, Actual: “2014-04-03T20:51:31.274Z”


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

I think the time wasn’t in the correct format

DateTime testDate = DateTime.now();
System.assertEquals(‘”‘ + testDate.formatGMT(‘yyyy-MM-dd’T’HH:mm:ss.SSS’Z” + ‘”‘), JSON.serialize(testDate));

Method 2

The DateTime.format() method converts the value to the time zone of the user who is running the test. The serializer is displaying the value as GMT.

A simple solution would be to use the DateTime.formatGMT() method in your test.

DateTime testDate = DateTime.now();
System.assertEquals('"' + testDate.formatGMT('yyyy-MM-dd'T'hh:mm:ss.SSS'Z'' + '"'), JSON.serialize(testDate));

Update: (The problem is in the format string as noted by @melmoussaoui – ‘HH’ vs ‘hh’)

DateTime testDate = DateTime.now();
String expectedValue = '"' + testDate.formatGMT('yyyy-MM-dd'T'HH:mm:ss.SSS'Z'' + '"');
String actualValue = JSON.serialize(testDate);
system.debug('Expected: ' + expectedValue);
system.debug('Actual:   ' + actualValue);
System.assertEquals(expectedValue, actualValue);

Debug output:

14:12:04:046 USER_DEBUG [4]|DEBUG|Expected: "2014-04-03T21:12:04.853Z"
14:12:04:046 USER_DEBUG [5]|DEBUG|Actual:   "2014-04-03T21:12:04.853Z"

Salesforce DateTime methods

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x