I do:

Date d = (Date)JSON.deserialize('2013-06-07T00:00:00.000Z', Date.class);
System.debug('**********d = ' + d);

I expect:

*************d = 2013-06-07 00:00:00

I see:

*************d = 2013-01-01 00:00:00

Please help.


Couple of problems
a. deserializing the date with a time part will throw an exception. You might want to try deserializing to a Datetime and extract the date from there.

b. Missing quotes. It should read:

Date d = (Date)JSON.deserialize('"2013-06-07"', Date.class);

Have you considered using DateTime.valueOf() or DateTime.valueOfGMT() instead of deserializing? They should accept your value as a parameter.



Going by the docs for Apex JSONParser methods, the following will parse out a DateTime for you:

String JSONContent = '{"transactionDate":"2013-06-07T00:00:00.000Z"}';
JSONParser parser = JSON.createParser(JSONContent);
// Advance to the start object marker.
// Advance to the next value.
// Get the transaction date.
Date transactionDate = parser.getDatetimeValue().date();
System.Debug('Date: ' + transactionDate);

Result (based on my timezone):

USER_DEBUG [12]|DEBUG|Date: 2013-06-06 00:00:00

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.