You need to sign in to do that
Don't have an account?
craigmh
Date field is cast as string with time from dynamic SOQL
Here is my code:
sObject obj = database.query('Select DateField__c From OpportunitySchedule__c Where Id = \'' + string.escapeSingleQuotes(objId) + '\' Limit 1'); date dtSelectedDate = date.valueOf(obj.get('DateField__c'));
DateField__c is of datatype date, not datetime.
When I try to run this code, this is the error that I get:
Invalid date: 2011-06-25 00:00:00
When I try to cast it as a datetime, this is the error that I get:
Invalid date/time: 2011-06-25 00:00:00
When the date is cast as a string, is it converted to a format that is impossible to get back to a date or date/time by any method other than manual parsing?
FYI, here's my hack fix, after the query:
string strDate = string.valueOf(obj.get('DateField__c')); strDate = strDate.substring(0, strDate.indexOf(' ')); date dtSelectedDate = date.valueOf(strDate);
Good fix .... Thankx for sharing it.............
try using just .format() as in
That should remove the 00:00 from your date.
Good luck!
Mike,
The format() method returns a string, so I get this error when trying to save the class:
Error: Compile Error: Illegal assignment from String to Date
Although I may try to play around with that a bit more.
craigmh:
I think your issue is that if you use system.debug to print a date (or otherwise expose a date to print as a string, APEX will format it with a default time of 00:00:00.
I replicated your code in my environment using my own date field as shown below:
and did Anonymous Apex execute getting these results: