I have the following object (invMappingNew):

{customerId__c=1, addressId__c=8212BJ154, invoiceType__c=AdvancePayment, invoiceTypeLocalized__c=Voorschot, invoiceDate__c=2015-02-13 00:00:00, paymentDueDate__c=2015-02-20 00:00:00, invoiceNumber__c=157005888, startDate__c=2015-03-01 00:00:00, endDate__c=2015-04-01 00:00:00, periodDescription__c=Maart 2015, amount__c=165.29, vatAmount__c=34.71, totalAmount__c=0.00, Id=a000X000015AAmsOOO} 

When I Json.serialize this I lose the 00:00:00 digits for date. I need to have these replaced as T00:00:00 after serializing. How do I achieve this? Is there a way I can Stop the removal of the 00:00:00 part with the serialization and on the string(stringListInvoice) do a replace(' 00:00:00','T00:00:00')?

My code

stringListInvoice = stringListInvoice + JSON.serialize(invMappingNew)+',';
  • 2
    You realize a Date field has no Time component? What is it you are trying to achieve, exactly?
    – Adrian Larson
    Jun 24, 2018 at 20:41
  • @AdrianLarson throw this in your anonymouse window:Date d = date.parse('24-06-2018'); system.debug('d '+ d); and debug. What I want to achieve is to replace the 00:00:00 with T00:00:00
    – Thomas
    Jun 24, 2018 at 20:54
  • 1
    Why? Please edit your post to clarify your end goal.
    – Adrian Larson
    Jun 24, 2018 at 21:02
  • I need to serialize this in a specific string for an API Call the only part that I am missing after the serialization is the T00:00:00 instead of 00:00:00. It seems like because of the space between 2015-02-20 00:00:00 the zero digits get removed after serializing?
    – Thomas
    Jun 24, 2018 at 21:08

1 Answer 1


By default the Date type does not have a formatting method to output the ISO format. It shouldn't even do that, since it does not contain the hour, minute, second and millisecond data.

You should use the Datetime class instead:


Which outputs the following:

18:05:07:002 USER_DEBUG [8]|DEBUG|"2018-06-24T21:05:07.899Z"

It should work with object serialization too:

public class CType {
    public Datetime dt;
    public String name;

    public CType (String n) {
        this.name = n;
        this.dt = Datetime.now();

System.debug(JSON.serialize(new CType('hello')));


18:07:59:003 USER_DEBUG [20]|DEBUG|{"name":"hello","dt":"2018-06-24T21:07:59.886Z"}

And finally, if you don't need the extra 'Z' at the end (which is part of the standard) or you need the date formatted in any other way, you could probably build the JSON dynamically, and replace the dates with something like this:


Although I would recommend following the standard and, if possible, adjusting the webservice that is receiving the API call to handle the ISO format.

  • Thanks! I thought about that but in my case im bound to the date field as this is a requirement of the org im working with
    – Thomas
    Jun 24, 2018 at 21:15
  • If you can assume that the time will always be 0 (for hours, minutes, seconds and milliseconds), you can just create a new Datetime with it, using Datetime dt = Datetime.newInstance(d, Time.newInstance(0, 0, 0, 0));, assuming d is your Date instance. Jun 24, 2018 at 21:16
  • 2
    Yes, just parse your object into an inner class that replicates most of the fields, but for dates converts them to datetimes. Then parse that. You don't need to declare any new fields, it's all in Apex. Jun 24, 2018 at 21:20
  • @CasparHarmer could elaborate more on this what would the annotation be?
    – Thomas
    Jun 24, 2018 at 21:22
  • Well if you have to send a record, you'd create a class to wrap it. When passing it as argument to the constructor, the class would store Datetime properties instead of Date ones. When you serialize this class, it would result into a JSON with the dates serialized in the ISO format (containing T00:00:00Z). Jun 24, 2018 at 21:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .