Find nearest date among several dates to a given date

前端 未结 5 1440
一个人的身影
一个人的身影 2020-12-10 14:58

I have a list of dates and a current date. How can I find the date which is nearest to the current date?

5条回答
  •  一个人的身影
    2020-12-10 15:04

    If you can use a Set instead of a List, put the dates in a NavigableSet such as TreeSet and use the methods lower and higher.

    NavigableSet dates = new TreeSet();
    // add some dates to dates
    Date now = new Date();
    Date highestDateUpUntilNow = dates.lower(now);
    

提交回复
热议问题