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:
System.debug(DateTime.now().format('YYYY-MM-DDThh:mm:ssZ'));
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(DateTime.now().format('YYYY-MM-DD hh:mm:ssZ'));
Output:
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.
Example:
[SELECT Id FROM Account WHERE Some_Date__c > 1999-01-01T23:01:01Z]