title: Find the nearest previous date
body: <p>I have a list of dates and a target date object. I need to find the nearest previous date(before the target date). I have tried the following but I don't think it will work for only previous date.</p>
  
  <pre><code>private Date getDateNearest(List&lt;Date&gt; dates, Date targetDate) {
      Date returnDate = targetDate;
  
      for (Date date : dates) {
          if (date.compareTo(targetDate) &lt;= 0 &amp;&amp; date.compareTo(returnDate) &gt; 0) {
              returnDate=date;
          }
      }
  
      return returnDate;
  } 
  </code></pre>