Skip to main content
2 of 3
added 451 characters in body
Davin Casey
  • 4.2k
  • 1
  • 21
  • 35

There's probably a neater way to do it but you could just serialize the date into a string like so:

Datetime oneyearago = System.today().addDays(-365);
String jsonDatetime = JSON.serialize(oneyearago);

Which outputs the specified date in the correct format:

2015-01-06T00:00:00.000Z

In response to your comment:

String OppJSON = '{"attributes":{"type":"Opportunity","url":"/services/data/v25.0/sobjects/Opport‌​unity/500E0000002nH2fIAE"},"Id":"500E0000002nH2fIAE","LastModifiedDate":"' + jsonDatetime + '"}';
Opportunity opp = (Opportunity) JSON.deserialize(OppJSON, Opportunity.class );

Basically just replace the hardcoded value with the variable.

P.S. There may a formatting issue there, but you get the idea.

Davin Casey
  • 4.2k
  • 1
  • 21
  • 35