title: How to find nearest week day for an arbitrary date?
body: <p>Is there an elegant way to find the nearest day of the week for a given date using JodaTime? I initially thought <code>setCopy()</code> would be it, but this sets the day to the particular day <em>in the same week</em>. Thus, if <code>ld</code> is <code>2011-11-27</code> and <code>day</code> is "Monday" the following function returns <code>2011-11-21</code>, and not <code>2011-11-28</code> as I want.</p>
  
  <pre><code>    // Note that "day" can be _any_ day of the week, not just weekdays.
      LocalDate getNearestDayOfWeek(LocalDate ld, String day) {
          return ld.dayOfWeek().setCopy(day);
      }
  </code></pre>
  
  <p>Desired output for various inputs:</p>
  
  <pre><code>2011-12-04, Monday    =&gt; 2011-12-05
  2011-12-04, Tuesday   =&gt; 2011-12-06
  2011-12-04, Wednesday =&gt; 2011-12-07
  2011-12-04, Thursday  =&gt; 2011-12-01
  2011-12-04, Friday    =&gt; 2011-12-02
  2011-12-04, Saturday  =&gt; 2011-12-03
  2011-12-04, Sunday    =&gt; 2011-12-04
  
  2011-12-05, Monday    =&gt; 2011-12-05
  2011-12-05, Tuesday   =&gt; 2011-12-06
  2011-12-05, Wednesday =&gt; 2011-12-07
  2011-12-05, Thursday  =&gt; 2011-12-08
  2011-12-05, Friday    =&gt; 2011-12-02
  2011-12-05, Saturday  =&gt; 2011-12-03
  2011-12-05, Sunday    =&gt; 2011-12-04
  </code></pre>
  
  <p>Below is a work-around I came up with that works for the particular constraints in my current situation, but I'd love to get help find a completely generic solution that works always.</p>
  
  <pre><code>    LocalDate getNearestDayOfWeek(LocalDate ld, String day) {
          LocalDate target = ld.dayOfWeek().setCopy(day);
          if (ld.getDayOfWeek() &gt; DateTimeConstants.SATURDAY) {
              target = target.plusWeeks(1);
          }
          return target;
      }
  </code></pre>