Rob
Rob

Reputation: 23

XPages ODA issue with getting a date value from a field in Java

I'm using ODA (which is wonderful) in my Java code but I'm having trouble with getting a date value from a field.

If I use:

DateTime lastApprDt = doc.getItemValue("LastApproved", Date.class);

then the parser objects and suggests "change type of lastApprDt to Date"

If I change the code to:

Date lastApprDt = doc.getItemValue("LastApproved", Date.class);

then the parser is happy but when I run the code I get an error:

[ODA::WARNING] Auto-boxing requested a com.ibm.security.util.calendar.BaseCalendar$Date but is returning a org.openntf.domino.impl.DateTime in item LastApproved for document id 992

I'm confused! If doc.getItemValue("LastApproved", Date.class) returns a Date type then why do I get the error?

Upvotes: 0

Views: 160

Answers (3)

Per Henrik Lausten
Per Henrik Lausten

Reputation: 21709

Make sure that the lastApprDt Date is of type java.util.Date (and not of type com.ibm.security.util.calendar.BaseCalendar.Date).

Upvotes: 4

xpages-noob
xpages-noob

Reputation: 1579

If you are sure that the field contains a date value you should be able to get the java.util.Date with

Date lastApprDt = doc.getItemValue("LastApproved").get(0).toJavaDate();

Upvotes: 0

Paul Stephen Withers
Paul Stephen Withers

Reputation: 15729

The first is failing because it's trying to pass a Date object (the output from getItemValue()) into a DateTime object (lastApprDate).

I'm not quite sure why it's choosing to retrieve it as a com.ibm.security.util.calendar.BaseCalendar.Date, I can't see any reference to that class in the ODA code. It's worth checking the import statements in your code to make sure it's not chosen com.ibm.security.util.calendar.BaseCalendar.Date as the relevant Date class it thinks you want to use. I suspect it may have done. If so, change the import statement to use java.util.Date.

The code for autoboxing Dates looks for specific classes and how to convert them. java.util.Date is the most obvious one it's expecting. I recently added java.sql.Date, I believe for the last base 9.0.1 and first FP8 versions. java.util.Calendar is the other one supported. New Java 8 Date classes like LocalDateTime may seem good candidates because they have better timezone handling, but it's not easy to convert the DateTime timezone to a Java timezone and the timezone in a DateTime is readonly, so it wouldn't work for autoboxing back at the moment.

You shouldn't need to pass the full class name as the second parameter, I've got code running that just passes Date.class. That's what makes me suspect that the parser suggestion has guessed at the wrong class you wanted and imported com.ibm.security.util.calendar.BaseCalendar.Date.

Upvotes: 0

Related Questions