String dtString = '"'+ String.valueOf(Date.today()) + 'T00:00:00.000Z"';
DateTime dt = (DateTime)JSON.deserialize(dtString, DateTime.class);
System.debug(dt); //This prints 2016-08-30 00:00:00
System.debug(dt.date()); //This prints 2016-08-29 00:00:00
System.assert(dt.date() == System.now().date());
Not sure what is wrong with the above code, it keeps failing. As you can see on line 3 and 4 the debug statement prints different values.
I referred to this question on how to properly de-serialize a datetime.
dtString
, if this is all hypothetical, etc? – Adrian Larson♦ Aug 30 '16 at 21:35Datetime.valueOf
would be more reliable. – Adrian Larson♦ Aug 30 '16 at 22:04Datetime.valueOf
isDateTime
but I need in string format which can be desrialized into Datetime. – javanoob Aug 30 '16 at 22:16