A Dev
A Dev

Reputation: 321

Java sorting a collection of datetime values

I'm pulling the date and time of emails using the notes.jar lotus notes api. When I add them to a collection, if add them like this:

Vector times = doc.getItemValueDateTimeArray("PostedDate");
    for (int j=0; j<times.size(); j++) {
      Object time = times.elementAt(j);
      if (time.getClass().getName().endsWith("DateTime")) {
          String Listadd = ((DateTime)time).getLocalTime();
          NotesDates.add((DateTime)time);

I get the error:

lotus.domino.local.DateTime cannot be cast to java.lang.Comparable

When I add the values as String the code runs, but I cannot sort the collection.

How can I sort the collection of dates and times to find the earliest and latest?

Upvotes: 3

Views: 446

Answers (2)

James Wierzba
James Wierzba

Reputation: 17548

You have a few options. Here are two.

  1. Use a collection of java.util.Date instead of lotus.domino.local.DateTime The lotus notes DateTime object has a method toJavaDate(), and a java.util.Date implements the Comparable interface for you

  2. Implement a custom comparator for lotus.domino.local.DateTime object. It would probably be easiest to convert the date to a java date again, and use it's compare method, instead of re-inventing the wheel.

Upvotes: 5

Davide Lorenzo MARINO
Davide Lorenzo MARINO

Reputation: 26926

Create a custom Comparator that take as input the DateTime of Lotus.

Inside the code compare the DateTime as you need, for example converting them to a standard java.util.Date or getting single elements (year, month, date...) and comparing them singularly.

Upvotes: 2

Related Questions