Java对日期时间值的集合进行排序

A D*_*Dev 3 java lotus-notes

我正在使用notes.jar lotus notes api来提取电子邮件的日期和时间.当我将它们添加到集合中时,如果像这样添加它们:

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);
Run Code Online (Sandbox Code Playgroud)

我收到错误:

lotus.domino.local.DateTime cannot be cast to java.lang.Comparable
Run Code Online (Sandbox Code Playgroud)

当我将值添加为String时代码运行,但我无法对集合进行排序.

如何对日期和时间的集合进行排序以找到最早和最新的?

Jam*_*zba 5

你有几个选择.这是两个.

  1. 使用java.util.Date的集合而不是lotus.domino.local.DateTime Lotus注释DateTime对象有一个方法toJavaDate(),java.util.Date为你实现了Comparable接口

  2. 为lotus.domino.local.DateTime对象实现自定义比较器.或许,这将是最容易的日期再次转换为Java日期,并用它来比较法,而不是重新发明轮子.