[SalesForce] Date field is also giving me time

I am needing some help with a query that contains a date. I am collecting this date through a VF page with the help of a birthday fake contact field, my class is something like this:
Controller:

public with sharing class MyCLass {

public Contact fakeCont { get; set; }
public String queryString { get; set; }
public MyCLass() {
    fakeCont = new Contact();       
}

public void doQuery(){
    queryString = 'SELECT id, Name FROM SObject WHERE dateExample = ' + fakeCont.Birthdate  ; 
}

}

VF Page:

<apex:inputField styleClass="form-control" value="{!fakeCont.Birthdate}"/>

even that the field in contact is mark as date after selecting it displays something like
2014-11-02 00:00:00

it is not supposed to be displayed like: 2014-11-02 ?
as this field is date and not datetime?

if I try to use something like

newDate = fakeCont.Birthdate.date();

I will get an error

Best Answer

If you bind the date field into the query rather than make it part of the query string you will avoid this problem. So your query string becomes:

 queryString = 'SELECT id, Name FROM SObject WHERE dateExample = :fakeCont.Birthdate';

Note that generally its better not to use strings in queries but instead make use of the compile-time checked static form:

Xyz__c[] sobs = [SELECT id, Name FROM Xyz__c WHERE DateField__c = :fakeCont.Birthdate];
Related Topic