+ Start a Discussion
wsmithwsmith 

Date/Time field type not converted to Datetime data type using sObject.get(...)

In the example below I am using the Opportunity object and a Date/Time field.


The Date/Time field type is not converted into a Datetime data typeIs this a bug?

Opportunity o = ...;
Map opportunityFieldMap = Schema.SObjectType.Opportunity.fields.getMap();
Schema.DescribeFieldResult currentStageDFR = opportunityFieldMap.get('mydatetimefield__c').getDescribe();
Object currentStageValue = o.get(currentStageDFR.getName());

Examining the field object I get the following values:

(currentStageValue instanceOf Datetime) == false
(currentStageValue instanceOf Date) == true
(currentStageValue instanceOf Time) == false
String.valueOf(currentStageValue) == '2009-03-20 22:34:00' // GMT time and seconds are truncated/lost

((Date) currentStageValue).format() == '3/20/2009'

 

If I try to convert it into a Datetime using a Strings (since I cannot access time info on a Date data type) it loses the correct time by ignoring AM/PM info and loses the seconds.


Datetime myDateTime = Datetime.valueOf(String.valueOf(currentStageValue)) == '2009-03-21 05:34:00' //added hours

Datetime myDateTime = Datetime.valueOfGmt(String.valueOf(currentStageValue)) == '2009-03-20 22:34:00'

Is there a way to get a correct Datetime data type using sObject.get(...) for a Date/Time field types that keeps seconds?