I have a problem to convert String to DateTime and Insert it to a DateTime field in SF.

I found out that this is work :

String dateee = '2015/03/10 05:03:20';
DateTime date_time = DateTime.valueOfGmt(dateee.replaceAll('/','-'));
someObject__c someObjectInstance = new someObject__c(someObject_Date__c=date_time);
insert someObjectInstance;

But I get the DateTime from a JSON (String) : "03/10/2015 05:03:20" (dd/mm/yyyy) And when I put this text inside the code above I get an error of:

System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, someObject: invalid date: Sat Apr 06 05:03:20 GMT 9: [someObject__c]

I tried the below code to format it :

String dateee = '03/10/2015 05:03:20';
DateTime a = DateTime.valueOfGmt(dateee.replaceAll('/','-'));

But it dosnt work..

I need to format it and insert it as a DateTime field. Can someone please help or post here some links? I tried to look for it but didn't find something valuable.

2 Answers 2


In JSON format, use the standard ISO format:


For example, from your original value:


Example code (Execute Anonymous):

public class JSONStructure {
  public DateTime startDate;
  public DateTime endDate;
String jsonExample = '{"startDate":"2018-11-25T03:00:00.000Z","endDate":"2018-11-25T05:00:00.000Z"}';
JSONStructure output = (JSONStructure)JSON.deserialize(jsonExample, JSONStructure.class);

With output looking like this:

USER_DEBUG [7]|DEBUG|JSONStructure:[endDate=2018-11-25 05:00:00, startDate=2018-11-25 03:00:00]

Note that the times are in GMT, so if your time values are not, some conversion will be required.

  • Hi Thank you for your response. At the JSON I get from the client the next format DD\MM\YYYY - I can get it also as : MM\DD\YYYY. I'm running the below code : ` String dateee = '03/10/2015 05:03:20'; DateTime a = DateTime.valueOfGMT(dateee.replaceAll('/','-')); ` (I tried to use the function just valueOf without the GMT..) and the output is : DEBUG|0009-04-06 05:03:20 and that why the insert of a DateTime field is throw me a DML Ecxeption - I dont sure why its converting it like this way.. and how can it be fix without use the struct : YYYY\MM\DD. thanks @sfdcfox
    – Salvation
    Commented Nov 27, 2018 at 6:07
  • @Salvation Let's try this again. If you want to use the JSON class to parse your JSON, use YYYY-MM-DDTHH:mm:ss.SSSZ. If you want to use some other format, you must manually parse the string. Which also means that you need to know your locale to use DateTime.parse(dateString), or you need to use DateTime.newInstance(year, month, day, hour, minute, second), which involves getting the six value parts, using Integer.valueOf to convert them to numbers, and use those to get the final value you'd assign.
    – sfdcfox
    Commented Nov 27, 2018 at 6:20
  • Thank you @sfdcfox, I just got it. I somehow confused and thought that if I will insert this format (YYYY-MM-DD) it will insert this format as well in SF DateTime field - But I just figured out that it depends on the local user setting and that why I didn't understand you in the first time. I will convert his date to this format and insert it to SF. Many thanks!!
    – Salvation
    Commented Nov 28, 2018 at 7:59

valueOf in place of DateTime.valueOfGmt

String dateee = '2015/03/10 05:03:20';

DateTime date_time = DateTime.valueOf(dateee.replaceAll('/','-'));

someObject__c someObjectInstance = new someObject__c(someObject_Date__c=date_time);

insert someObjectInstance;

hope this help you.

  • Yes, that worked for me also as I wrote in the post. But I get the date in different format: dd/mm/yyyy and here the issue begins - and I get the DML error. I don't want to make a function for changing the date to this format - I think there is a better and easy way.. I just didn't find it yet :\ Thanks ! @Nagendra Swami
    – Salvation
    Commented Nov 25, 2018 at 11:40
  • the date format depends on user locale setting when you will go to my setting and locale text then there should united states by default that allow (MM/DD/YYYY) format. if you need different format then update the locale setting. Commented Nov 25, 2018 at 11:52
  • use this 1. At the top of any Salesforce page, click the down arrow next to your name. From the menu under your name, select Setup or My Settings—whichever one appears. 2. From the left panel, select one of the following: If you clicked Setup, select My Personal Information | Personal Information. If you clicked My Settings, select Personal | Advanced User Details.& Time Zone. 3. Click Edit. 4. Change the locale to the desired region. Commented Nov 25, 2018 at 11:54
  • Thank you! I got my mistake here as I replayed to sfdcfox. Thanks !!
    – Salvation
    Commented Nov 28, 2018 at 8:01
  • Can't Mark 2 answers as best answers but this one helped me also. Thank you!
    – Salvation
    Commented Nov 28, 2018 at 8:03

You must log in to answer this question.

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