How to convert string to Date in apex?

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 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 use this link to read about more date properties.


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.