Use a Calendar
:
- Set
YEAR
to 2009
- Set
DAY_OF_YEAR
to 1
- Iterate over all days in year 2009, 2010 checking for Mon-Thu.
Code:
Calendar cal = Calendar.getInstance();
// Start in 1 Jan 2009
cal.set(YEAR, 2009);
cal.set(DAY_OF_YEAR, 1);
// Iterate while in 2009 or 2010
while (cal.get(YEAR) <= 2010)
{
int dow = cal.get(DAY_OF_WEEK);
if (dow >= Calendar.MONDAY && dow <= Calendar.THURSDAY))
{
// add to your map
}
cal.add(Calendar.DATE, 1);
}
Update:
It is trivial to optimize this so that you don't need to iterate over Fri, Sat, Sun: Just add 4 days whenever you see a Thursday, 1 otherwise:
while (cal.get(YEAR) <= 2010)
{
int dow = cal.get(DAY_OF_WEEK);
if (dow >= Calendar.MONDAY && dow <= Calendar.THURSDAY))
{
// add to your map
}
cal.add(Calendar.DATE, (dow == Calendar.THURSDAY)? 4 : 1);
}