Thursday 26 March 2015

System.TypeException: Invalid date in Salesforce

        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


  1. String str = '03/26/2015';
  2. Date mydate = Date.parse(str);
  3. System.debug(mydate);

Locale --> English (India) --> String format should be dd/mm/yyyy

  1. String str = '26/03/2015';
  2. Date mydate = Date.parse(str);
  3. 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


  1. string year = '2008';
  2. string month = '10';
  3. string day = '5';
  4. string hour = '12';
  5. string minute = '20';
  6. string second = '20';
  7. string stringDate = year + '-' + month
  8.  + '-' + day + ' ' + hour + ':' +
  9. minute + ':' + second;
  10. Date myDate = date.valueOf(stringDate);


Refer more at,
http://www.salesforce.com/us/developer/docs/dbcom_apex230/Content/apex_methods_system_date.htm

1 comment:

Activities: Assign Tasks to a Queue Salesforce Lightning

Salesforce announced to assign Tasks to a Queue beginning from Spring'20 release. How does it work? In Setup, enter Queues in th...