[SalesForce] Date format validation

i need to verify the date format in if condition,

date is coming in "YYYY-MM-DD hh:mm:ss"(2015-15-10 00:00:00) format from java, i need to validate date format in if condition, if date is not in this format , i need to display error message " invalid date format

Ex :

for( obj aa: listrec)
     if(obj.date != null && check date format above)

     obj.errr= ' invalid date format or empty"


Can any one please explain me how can i validate date format in salesforce.


Best Answer

Unfortunately DateTime doesn't seem to have a "parseISO" method that parses ISO-formatted dates, so you can't take advantage of something like that. The obvious solution here seems to be regex:

if(String.isNotBlank(obj.date) &&  
   Pattern.matches('\\d\\d\\d\\d-\\d\\d-\\d\\d \\d\\d:\\d\\d:\\d\\d', obj.date))
Related Topic