I'm not seeing a good way to set a date to a certain day of the week for a certain week of the month. Joda-Time's LocalDate does not have a withWeekOfMonth method. I can see a possible algorithm, but it seems way to complicated, so I'm going to assume I'm missing something. What I need is to determine the next date someone is paid. And if they are paid on the Second Thursday of the Month, what date is that.
Anyone already solved this problem?
Ok, I was able to come up with this, which seems to work fine.
/**
* Finds a date such as 2nd Tuesday of a month.
*/
public static LocalDate calcDayOfWeekOfMonth( final DayOfWeek pDayOfWeek, final int pWeekOfMonth, final LocalDate pStartDate )
{
LocalDate result = pStartDate;
int month = result.getMonthOfYear();
result = result.withDayOfMonth( 1 );
result = result.withDayOfWeek( pDayOfWeek.ordinal() );
if ( result.getMonthOfYear() != month )
{
result = result.plusWeeks( 1 );
}
result = result.plusWeeks( pWeekOfMonth - 1 );
return result;
}