Bill F
Bill F

Reputation: 2087

Getting a Notes Dateime from a ViewEntry into a JAVA Date

I have this block of code in a Java Method:

colVal = ve.getColumnValues();
    System.out.println("Got colVal");
    System.out.println("Col values = " + colVal.toString());
    try {
        Document pDoc = ve.getDocument();
        System.out.println("Start MyDate");
        DateTime dt = (DateTime) pDoc.getItemValueDateTimeArray("ExpPayDate").get(0);
        Date pDate = dt.toJavaDate();
        pItem.setMyDate(pDate);
    } catch (Exception e) {
                        // date error
    System.out.println("setMyDate Failed "+ e.toString());
    }

The log looks like this:

12/09/2015 02:49:59 PM  HTTP JVM: Got colVal
12/09/2015 02:49:59 PM  HTTP JVM: Col values = [1bp8frg61ze9s, 24/09/2015 12:00:00 PM MDT, , 0.0, ---, , --- No Agent ---, , ]

I use the ViewEntry because I need to maintain the view sort order. The problem with this is that the ve.getDocument() means an extra trip to the server and is could become fairly expensive. So I have tried to work just with the colVal.get(1), it is viewed as a Notes DataTime but colVal.get(1).toJavaDate() does not seem to be available.

Changed the code to try to get the value from the colVal:

System.out.println("Got colVal");
System.out.println("Col values = " + colVal.toString());
    try {
     System.out.println("Start MyDate");
    System.out.println("Get value from colVal " + colVal.get(1).toString());
                        //pItem.setMyDate(pDate);
    } catch (Exception e)
        System.out.println("setExpPayDate Failed "  + e.toString());
} 

When I run this "get value from colVal " causes an error :

12/09/2015 03:05:55 PM  HTTP JVM: Start MyDate
12/09/2015 03:05:55 PM  HTTP JVM: setMyDate Failed java.lang.ClassCastException: lotus.domino.local.DateTime incompatible with java.lang.String

I can do what I need from the document but can't seem to get there from the viewEntry and the ve.getColumnValues().

Upvotes: 1

Views: 621

Answers (1)

Jesse Gallagher
Jesse Gallagher

Reputation: 4471

The immediate solution is that you need to cast the object to a DateTime to be able to call the toJavaDate method. For example:

DateTime dt = (DateTime)colVal.get(1);
Date date = dt.toJavaDate();

This is because the Vector returned by getColumnValues returns Object-typed values, since it can be mixed.

In this specific case, there's also a way to get the column value as a Date directly. If you call entry.setPreferJavaDates(true) before getting the column values, then date/time values will be represented as Date objects instead of DateTime. You'd still probably end up casting the column value to Date anyway, but that can be a small convenience. It also eliminates the need to do entry.recycle(colVal) inside the loop to make sure the DateTimes in the column values are recycled.

Upvotes: 6

Related Questions