<p>I have a list of Date objects, and a target Date. I want to find the date in the list that's nearest to the target date, but only dates that are before the target date.</p>

<p>Example:
2008-10-1
2008-10-2
2008-10-4</p>

<p>With a target date of 2008-10-3, I want to get 2008-10-2</p>

<p>What is the best way to do it?</p>

I currently use the following method, but I'm not sure it's the most effective one, because this assumes an already sorted list, and (potentially) iterates over every single date in the list.
```
```
private Date getDateNearest(List<Date> dates, Date targetDate){
  for (Date date : dates) {
    if (date.compareTo(targetDate) <= 0) return date;
  }

  return targetDate;
}
```
```