如何从对象中提取最新日期(日期列表,Java)

时间:2017-12-20 17:08:50

标签: java sorting date collections

我有一个以下形式的对象list_of_dates

[2018-06-18T00:00Z, 2018-06-23T00:00Z, 2018-06-02T00:00Z,...,2018-06-21T00:00Z]

我想提取最新日期。我不能在以下的帮助下做到这一点:

//here, I assume object is transformed into list and then sorted.
Collections.sort(Arrays.asList(list_of_dates)); 
return list_of_dates[list_of_dates.length-1]; 

你能说明我为什么会收到错误吗?

1 个答案:

答案 0 :(得分:2)

您的代码应该可以正常工作,这是另一种解决方案,您可以在其中找到maxmin

ZonedDateTime[] list_of_dates = new ZonedDateTime[n];
//...
ZonedDateTime maxDate = Stream.of(list_of_dates).max(ZonedDateTime::compareTo).get();
相关问题