Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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'm trying to get a DateTime to format into a value to be used in a dynamic query. For some reason, the documentation snippet on how to format a DateTime for a query is giving me an exception.

Im trying to run this anonymous apex snippet:


The format string I'm using is specified on Date Formats and Date Literals.

The exception I'm getting back is:

Line: 1, Column: 1 System.StringException: Unrecognized format: YYYY-MM-DDThh:mm:ssZ

If I remove the T, the string is accepted, but isn't in the right format to query on.

System.debug('YYYY-MM-DD hh:mm:ssZ'));


2016-03-75 04:01:33-0400

Additionally, if I take the date literals provided on the page, and use them in a query, the query accepts the string, but still won't let me try to format a DateTime into the same Date Format as the accepted Date Literal.


[SELECT Id FROM Account WHERE Some_Date__c > 1999-01-01T23:01:01Z]
share|improve this question
Why are you formatting DateTime values instead of just binding directly to the variable? – sfdcfox Mar 15 at 21:12
The formatted date string is being used later on in a partner api query operation, so I can't use a bind variable inside the query string. – battery.cord Mar 16 at 14:42
up vote 8 down vote accepted




The esteemed Mr Ballinger also recommends the following, but it is wrapped in double quotes and slower, so I would probably not use it, myself.

share|improve this answer

You are linking into formats used in SOQL engine. Please refer to Apex reference and Java SimpleDateFormat for more advanced formats.

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.