To convert String to Date in salesforce

The below simple code is used to convert String to Date data type using Apex

Sample Code:

  1. String todate = '12/27/2013';  
  2. Date dt = Date.parse( todate );  
  3. system.debug(' Parsed Date is ' + dt );  

Output:



7 comments:

  1. it occur runtime error Invalid date

    ReplyDelete
  2. Use Date.parse(todate) instead

    ReplyDelete
  3. Use :

    String todate = '12/27/2013';
    Date dt = Date.parse(todate);

    ReplyDelete
  4. showing error

    ReplyDelete
  5. how to handle the issue while converting empty string. And set date as empty.

    ReplyDelete
    Replies
    1. Use String.isNotBlank() before checking against the string.

      Delete