+1 vote
31 views
by (163k points)

I have created this but getting an error>> "Condition expression must be of type Boolean: String"

public static boolean validateDateFormat(String date1){
    DateTime validDate  = DateTime.valueOf((date1);
        if(validDate.format('mm/dd/yyyy')){
            return true;
        }
        else{
            return false;
        }
        
}

1 Answer

+1 vote
by (163k points)
 
Best answer

Please check the documentation for the format, its return type is String. and as error message is depicting the if conditions requires TRUE/FALSE.

Also please add the negative scenarios in the methods(what if string is not date).

Documentation link - https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_datetime.htm

A rough code for checking pattern is -

String regex = '[0-9]{0,2}/[0-9]{0,2}/[0-9]{4}';
Pattern datePattern = Pattern.compile(regex);
Matcher matchDate = datePattern.matcher('1/11/2020');
System.assert( matchDate.matches() == true);
matchDate = datePattern.matcher('1/111/2020');
System.assert( matchDate.matches() == false);
Welcome to Memory Exceeded, where you can ask questions and receive answers from other members of the community.
You can donate any amount for Orphans village using following QR Code.
...