function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ALimas22ALimas22 

System.TypeException: Invalid conversion from runtime type Date to Datetime

I need to get the milliseconds of the "CreatedDate" field for a case in my Apex controller extension class.  The code looks like this:

 

long creationDateInMillis = ((DateTime)aCase.get('CreatedDate')); // aCase is the object that's passed in to the constructor

 

When the method that contains this line gets invoked, that line causes the error "System.TypeException: Invalid conversion from runtime type Date to Datetime".

 

According to the documentation, "CreatedDate" is of type  "DateTime", so I'm confused as to why the above casting would not work and why it thinks "CreatedDate" is a "Date" instead of a "DateTime"?

 

I'd like to know what I'm doing wrong here.  I need the accuracy down to the milliseconds, so cannot just cast to "Date" as that'll loose the time component.

 

Thank you.

Best Answer chosen by Admin (Salesforce Developers) 
XactiumBenXactiumBen

Try this:

 

Integer creationDateInMillis = aCase.CreatedDate.millisecond();

 

All Answers

XactiumBenXactiumBen

Try this:

 

Integer creationDateInMillis = aCase.CreatedDate.millisecond();

 

This was selected as the best answer
ALimas22ALimas22

Thank you very much.  Such a simple syntax. :robothappy:

 

I didn't properly state the need (and didn't see there was a "DateTime.millisecond()" until the posted reply) as I really need the whole "DateTime" value converted to include milliseconds and I didn't include the call to "DateTime.getTime()" in the code snippet in my original posting.