Java-Joda时间列表< LocalDate>查找最新日期 [英] Java - Joda time List<LocalDate> find latest date

查看:79
本文介绍了Java-Joda时间列表< LocalDate>查找最新日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是使用Joda时间API的日期列表,我想从下面的列表中找到最新的日期

following are the List of Dates by using Joda time API, i want to find the latest Date from the Below List

List<LocalDate> dates = new ArrayList<LocalDate>();
dates.add(eefdTewntyArray)
dates.add(rdTewntyArray);
dates.add(idSeventyArray);

以上列表的输出

[2025-08-01, 2025-08-01, 2026-08-01]

我想选择最新日期并存储到LocalDate中,这也可能会增加日期的n个.

i want to choose the latest date and stored into an LocalDate, it may increase the 'n' number of date also

推荐答案

为此使用 Collections.max .由于每个 LocalTime 都实现了 Comparable ,因此这样做安全且简单.

Use Collections.max for this one. Since each LocalTime implements Comparable, this is safe and straightforward to do.

List<LocalDate> dates = new ArrayList<>();
dates.add(new LocalDate(2025, 8, 1));
dates.add(new LocalDate(2025, 8, 1));
dates.add(new LocalDate(2026, 8, 1));

// prints "2006-08-01"
System.out.println(Collections.max(dates));

Collections#max 在线性时间内运行,其最坏情况下的性能为O(n),而

Collections#max runs in linear time with a worst-case performance of O(n), whereas Collections#sort is based on Timsort, which has a worst-case runtime of O(n log n). If your list of dates becomes sufficiently large, then and only then would I consider using the sort approach; here, it's a bit overkill.

这篇关于Java-Joda时间列表&lt; LocalDate&gt;查找最新日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆