12

What is wrong with this format while am upserting the date in an object?

    Route__c r =new Route_c();
    r.Plan_Date__c = date.parse(strDate);
    upsert r;

In the debug log, I get the error as |FATAL_ERROR|System.TypeException: Invalid date: Test123

Here Test123 is the different text to be inserted which accepts as Date and throws an error.

3
  • 1
    so does it work fine if you give a valid date like '01/14/2016'?
    – AslamK
    Jan 14, 2016 at 10:35
  • you'll be much happier if all incoming dates are in yyyy-mm-dd format to avoid locale-specific issues
    – cropredy
    Jan 14, 2016 at 16:07
  • 2
    Why do you think 'Test123' is a valid date in any locale?
    – Eric
    Sep 6, 2016 at 0:12

2 Answers 2

24

This error is coming as system is not able to understand Test123 while parsing into date.

The format of the String depends on the local date format, like mm/dd/yyyy. If the parameter is not of this format, date.parse will throw an error.

E.g.:

r.Plan_Date__c = date.parse('12/27/2015');

You can also use date.valueOf(strDate) to parse string of format yyyy-MM-dd HH:mm:ss in the local time zone.

Please read about more date properties.

19

In case this is helpful to anyone, here is the Apex code to convert a string containing a date into an instance of the standard Date class (without going through the Datetime class or doing anything else funny). Note: It's important that the string be in YYYY-MM-DD format.

Date x = Date.valueOf('2015-12-31');

You can read more about the Date class here: Date Class | Apex Developer Guide.

You must log in to answer this question.

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