Hope most of the peoples may across such issue “System.TypeException: Invalid date”, while converting String to Date format. This article for how to convert string to date.
Parse Method
Constructs a Date from a String. The format of the String depends on the local date format.
Note: Parse method based on User locales.
Examples based on Locale
Locale –> English (United States) –> String format should be mm/dd/yyyy
-
String str = ’03/26/2015′;
-
Date mydate = Date.parse(str);
-
System.debug(mydate);
Locale –> English (India) –> String format should be dd/mm/yyyy
-
String str = ’26/03/2015′;
-
Date mydate = Date.parse(str);
-
System.debug(mydate);
If you provide wrong string format, then you will receive an System.TypeException Invalid Date Error.
ValueOf Method
Returns a Date that contains the value of the specified String. The String should use the standard date format “yyyy-MM-dd HH:mm:ss” in the local time zone.
Example
-
string year = ‘2008’;
-
string month = ’10’;
-
string day = ‘5’;
-
string hour = ’12’;
-
string minute = ’20’;
-
string second = ’20’;
-
string stringDate = year + ‘-‘ + month
-
+ ‘-‘ + day + ‘ ‘ + hour + ‘:’ +
-
minute + ‘:’ + second;
-
Date myDate = date.valueOf(stringDate);
Refer more at,
http://www.salesforce.com/us/developer/docs/dbcom_apex230/Content/apex_methods_system_date.htm