Database.getUpdated() returns a GetUpdatedResult object, which has a getLatestDateCovered() method. This method returns a Date object. When I do a System.debug to view the returned Date, I see a date and a time:

DEBUG|result of GetUpdatedResult.getLatestDateCovered(): 2016-09-09 04:39:00

However, since this is a Date and not a Datetime, I can't seem to access the time element of the object (there are obviously no hour() or minute() methods on the Date class, and when I try to convert the Date to a String using format(), it drops the time). I would like to store the full Datetime value and use it as the 'startDate' parameter when I make my next call to Database.getUpdated.

I have a feeling I'm missing something obvious. How can I create a Datetime object that includes the date AND time returned from GetUpdatedResult.getLatestDateCovered()?

  • did you tried datetime.parse(datetimeString) or Datetime.valueOf(datetimeString) – Tushar Sharma Sep 9 '16 at 5:32

Unfortunately you can't. As it is stated in Primitive Types doc:

Date: A value that indicates a particular day. Unlike Datetime values, Date values contain no information about time. Date values must always be created with a system static method.

getLatestDateCovered() returns Date, you can create a DateTime as follows:

Date dateInst = getUpdatedResult.getLatestDateCovered();
Datetime dt = datetime.newInstance(dateInst.year(), dateInst.month(), dateInst.day());

However, as you may guess, you can't keep time information since Date class doesn't provide a way to do it.

The fact that you can see time using System.debug(dateInst) is one of those weird things that only Apex-Compiler devs could explain. However, this quote from Date class doc gives a hint that Date instances store (or they did at least) time info:

In API version 33.0 or earlier, if you call Date.valueOf with an object that represents a Datetime, the method returns a Date value that contains the hours, minutes, and seconds. In version 34.0 and later, Date.valueOf converts the object to a valid Date without the time information. To convert a variable of type Datetime to a Date, use the Datetime.date method.

Even if Date contains time info, currently this class doesn't provide a way to access to that information.

In the case of Database.getUpdated(objectName, startDate, endDate), startDate and endDate are handled as DateTime objects, which can include hours, minutes and seconds. If you actually need the time that you see in the debug logs, that's actually the time value of endDate, so you can just get that time from that argument.

DateTime startDate = DateTime.newInstanceGMT(2016, 9, 1, 1, 0, 0);
DateTime endDate = DateTime.newInstanceGMT(2016, 9, 2, 6, 0, 0);
Database.GetUpdatedResult r = Database.getUpdated('Opportunity', startDate, endDate);

// |DEBUG|2016-09-02 06:00:00

system.debug(endDate.formatGMT('yyyy-MM-dd HH:mm:ss'));
// |DEBUG|2016-09-02 06:00:00
  • Thanks @Joca, this is very helpful. I don't understand why the GetUpdatedResult object would return a Date and not a Datetime from the getLatestDateCovered() method, but it is good to know that the endDate parameter passed in has the same Datetime value - so I can use that in my design. Thanks again! – kenslewis Sep 9 '16 at 16:08
  • You're welcome, and yeah, I also think that there are some things in apex libraries that don't make too much sense. – Joca Sep 9 '16 at 16:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.