Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have been trying to write a dynamic soql query by using soql-builder

But, I am stuck while assigning date as criteria while constructing the query.

Here is the code.

Date endOfDate =;
Date startOfDate =;

Time startTime = Time.newInstance(0, 0, 0, 0);
Time endTime = Time.newInstance(23, 59, 59, 0);

DateTime startDate = DateTime.newInstance(startOfDate ,startTime);
System.debug('DEBUG: StartDate : '+startDate); // DEBUG: StartDate : 2014-03-07 05:00:00

DateTime endDate = DateTime.newInstance(endOfDate ,endTime);
System.debug('DEBUG: EndDate : '+endDate); //DEBUG: EndDate : 2014-04-09 03:59:59 

But, after query constructed by using soql-builder. Debug came like this :

(CreatedDate >= 2014-03-07T12:00:00Z AND CreatedDate <= 2014-04-07T11:59:59Z)


startdate came 2014-03-07T12:00:00Z

endDate came 2014-04-07T11:59:59Z

Expected :

startdate 2014-03-07T00:00:00Z

endDate 2014-04-07T23:59:59Z

Anyone please give some light, why output is coming like this in soql-builder.

share|improve this question
up vote 7 down vote accepted

What's happening is that you're creating the times in your local time zone, but getting the string value in UTC. When I run your code, I get

StartDate : 2014-03-07 08:00:00
EndDate : 2014-04-08 06:59:59

because I'm on Pacific Time.

To get the results you're expecting, change DateTime.newInstance() to DateTime.newInstanceGmt(). This gives me the result

StartDate : 2014-03-07 00:00:00
EndDate : 2014-04-07 23:59:59
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.