For instance in Sept I would need:
1,8,15,22 and 29
Thanks
For instance in Sept I would need:
1,8,15,22 and 29
Thanks
Look up the Doomsday Rule - you should be able to apply that algorithm to your problem.
The simplest (naive) solution I can see is:
Get a Calendar (Calendar.getInstance())
Set the year, month, date etc (zero out other fields).
Then iterate adding one to the date (stop if you're not in the correct month - calendar.get(Calendar.MONTH))
while iterating, if calendar.get(Calendar.DAY_OF_WEEK) == Calendar.TUESDAY then increment the Tuesday counter.
As per daveb answer I've got:
import java.util.Calendar;
import static java.util.Calendar.*;
public class DiasDelMes {
public static void main( String [] args ) {
Calendar calendar = getInstance();
calendar.set( DAY_OF_MONTH, 1 );
int month = calendar.get( MONTH );
while( calendar.get( MONTH ) == month ) {
if( calendar.get( DAY_OF_WEEK ) == TUESDAY ) {
System.out.println( calendar.get( DAY_OF_MONTH ) ) ;
}
calendar.add( DAY_OF_MONTH , 1 );
}
}
}