2015-08-28 423 views
3

我使用notes.jar lotus notes api提取電子郵件的日期和時間。當我將它們添加到集合中,如果添加它們是這樣的:Java對日期時間值集合進行排序

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); 

我得到的錯誤:

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

當我添加的值作爲字符串代碼運行,但我不能排序的集合。

如何排列日期和時間的集合以查找最早和最新的?

+3

你需要寫一個比較''' –

+2

VECTOR'和Lotus Notes,還沒有看到那些在很長一段時間.. –

+1

只是想知道:爲什麼'time.getClass()的getName()。 endsWith(「DateTime」)'而不是'time instanceof DateTime'? – RealSkeptic

回答

5

您有幾個選項。這裏有兩個。

  1. 使用java.util.Date代替lotus.domino.local.DateTime 蓮花的集合票據DateTime對象有一個方法toJavaDate()和java.util.Date實現了Comparable接口,讓

  2. 爲lotus.domino.local.DateTime對象實現自定義比較器。將日期再次轉換爲java日期可能最簡單,並使用它的比較方法,而不是重新創建輪子。

2

創建一個自定義Comparator,輸入Lotus的DateTime

代碼內部根據需要比較DateTime,例如將它們轉換爲標準java.util.Date或獲取單個元素(年份,月份,日期...)並單獨比較它們。

相關問題