[SalesForce] Convert String Date to Date in Apex

I am using an SOQL in a webservice that is querying the Contact object based on the BirthDate field. The webservice request has the date in the format of 'yyyy-MM-dd',i.e.,1962-01-29 as an example.

I am not able to use this value to query the Contact Object. I tried converting this String Date into Date object. However, it always has 00:00:00 at the end. I tried the below options.

String strDate = '1962-01-29';
Date birthDate = Date.valueOf(strDate) ==> This results in 1962-01-29 00:00:00

String strDate = '1962-01-29';
Date dob = Date.valueOf(strDate)
Datetime dt = Datetime.newInstance(dob.Year(),dob.Month(),dob.day());
Date birthDate = dt.date() ==> This also results in 1962-01-29 00:00:00

How do I construct a Date object (without time) from the string date '1962-01-29'?

I did search for solutions in various forums and most of them are suggesting the above two solutions only, which does not work. I am not sure whether this behavior was changed in some Salesforce's releases.

Below is the full query that I am using.

                String query = 'SELECT id,' +
                                        'LastName,' +
                                        'FirstName,' +
                                        'BirthDate,' +
                                        'MiddleName__c,' +
                                        'MailingAddress,' +
                                        'OtherAddress,' +
                                        'EmploymentStatus__c,' +
                                        'Suffix__c,'+
                                        'Investment_Experience__c ' +
                                'FROM Contact WHERE';
                // Fetching the details of contact based on LastName,FirstName,BirthDate
                if(string.isNotEmpty(lastName)){ //Checking for last name value
                  if(lastName.contains('*') || lastName.contains('%')){
                    lastName = (lastName).replace('*','%');
                    query += ' LastName LIKE \''+lastName+'\' AND';
                  }
                  else{
                    query += ' LastName = \''+lastName+'\' AND'; 
                  }
                }
                if(string.isNotEmpty(firstName)){
                 if(firstName.contains('*') || firstName.contains('%')){
                    firstName = (firstName).replace('*','%');
                    query += ' FirstName LIKE \''+firstName+'\' AND';
                  }
                  else{
                    query += ' FirstName = \''+firstName+'\' AND';   
                    }
                }
                if(string.isNotEmpty(dateOfBirth)){
                    //Datetime dt = Date.valueOf(dateOfBirth);
                    //Datetime dob = Date.newInstance(dt.year(), dt.month(), dt.day());
                    query += ' BirthDate = ' + Date.valueOf(dateOfBirth) + ' AND';
                }
                query += ' Account.OrgNumber__c IN (\''+(string.join(lstOrganizationNumbers,'\',\''))+'\') ';
                query += ' ORDER BY lastName,firstName,BirthDate';
                query += ' LIMIT 50';
                system.debug('Query:: '+query); 

Best Answer

An instance of Date has no Time attributes, unlike Datetime. However, if you debug it, the output will show one. This debugging result has no impact on how the value will be used in SOQL.

Your problem is not how you are constructing your date, but likely how you construct your SOQL (which you haven't shown us). If you are building a dynamic query in Apex, you can just bind to a variable in memory (as long as you use no dot reference):

Date dateValue = Date.valueOf('1962-01-29');
String soql = 'SELECT Id FROM My_Object__c WHERE Date_Field__c = :dateValue';