+ Start a Discussion

Convert String to Date without 00:00:00

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.

I even tried the Date.parse() method. Whatever I do, I always get the Date with 00:00:00 at the end and this is messing up the SOQL query. When the SOQL is executed, I am getting a message saying [Exception: Line 1:343 No Viable Alternative at character '' ]. The SOQL is built as shown below.
USER_DEBUG [112]|DEBUG|Query:: SELECT id,LastName,FirstName,BirthDate,MiddleName__c,MailingAddress,OtherAddress,EmploymentStatus__c,Suffix__c,MaritalStatus__c,Gender__c,MailingState__c,HomePhone,MobilePhone,Email FROM Contact WHERE LastName LIKE 'Smith%' AND BirthDate = 1962-01-29 00:00:00  ORDER BY lastName,firstName,BirthDate LIMIT 50

If I run this query in workbench, I get the same error message.

Can someone help?
Hi Balaji,

You can still query the contact record from Salesforce with following:

The date doesn't have to be in this format -- '1962-01-29'
It can definitely be in this format -- 1962-01-29 00:00:00

Her we go,
String strDate = '1962-01-29';

Date myDate = Date.valueOf(strDate);

system.debug('myDate: ' + myDate);

List<Contact> myCons = [Select Id, Name, BirthDate FROM Contact WHERE BirthDate =: myDate];
system.debug('myCons size: ' + myCons.size());
system.debug('Person name: ' + myCons[0].name);
system.debug('Person birthdate: ' + myCons[0].BirthDate);

Output for debug log is below,

User-added image

Hope this helps!
Hi Harshil,

Yes, this is how it is supposed to work. However, I am getting the error message "No viable alternative character at '' ". Is it because I am constructing  the entire query in a String and finally calling the Database.query() method?
Why would you convert string into the date? Please share a code, I will take a look.

Below is working in workbench for me,
SELECT id,Name FROM Contact WHERE LastName LIKE 'Smith%' AND BirthDate = 1962-01-29 ORDER BY lastName,firstName,BirthDate LIMIT 50

Result is:

Id                                           Name
10031I000002CTDnQAO      Andy Smith
Hi, Thanks for the response. If you look at my SOQL, it has the date as 1962-01-29 00:00:00. The issue is resolved now. I have changed the date to a merge value and used the variable as :dob and it is working now. Thanks, Balaji.
Raj VakatiRaj Vakati
Can u give me code where you are calling Database.query() 
Raj VakatiRaj Vakati
Try this code
DateTime cutOff = currentTime.addHours(-2);
        String formatedDt = cutOff.format('yyyy-MM-dd\'T\'HH:mm:ss\'Z\'');
        String query =	  ' SELECT Id FROM From Case Where  CreatedDate <= '+formatedDt ;