[SalesForce] How to resolve the Invalid Date error in Apex while uploading CSV file

I am trying to upload the CSV file via Apex programming I am getting an error like Invalid Date Error. Please find the screenshot for more details. I have given code like this

site.Approved_Date__c =date.parse(inputsitevalues[13]);//date field [12].

Even though I have changed the date format as dd/mm/yyyy in CSV file I am getting the same error. Can anyone help me out in this issue.

enter image description here

Best Answer

It looks like the CSV file has the dates in mm/dd/yyyy format rather than dd/mm/yyyy. Hence the invalid date exception with the 31st month.

This will work:

date parsedDate = date.parse('31/5/2013');

This will fail with "System.TypeException: Invalid date: 5/31/2013"

date parsedDate = date.parse('5/31/2013');

From the Date.parse methods docs:

Constructs a Date from a String. The format of the String depends on the local date format. The following example works in some locales:
date mydate = date.parse('12/27/2009');

You can find the locale Date format information in the Supported Locales docs.

Another option would be to use the date format YYYY-mm-dd and date.valueOf().

Related Topic