I'm not wanting you to do anything. I'm
suggesting that you print the appropriate number of spaces before you begin printing the dates. For example, if the first day of the week happens to be a Wednesday, then I would add an appropriate number of spaces before printing the '1' for Wednesday, something like:
// shows how to skip spaces before printing a date
public class TestClass4
{
// a simple main() method that prints spaces and a date
public static void main( String[] args )
{
StringBuilder weekDates = new StringBuilder();
// print a header for the month
System.out.println( " S M T W T F S ");
// add extra spaces to start the first week of the month,
// start with 3 days for each day after Sunday
// Sun = 0, Mon = 1, Tue = 2, Wed = 3, . . .
// assuming my first month begins on Wed, firsDayOfMonth = 3
int firstDayOfMonth = 3;
// then set up a loop to build the string for the first week
for ( int i = 0 ; i < firstDayOfMonth ; i++ )
{
// add 3 spaces for each empty day of first week
weekDates.append( " " );
}
// then add the dates for the rest of the first week
for ( int i = 1 ; i <= ( 7 - firstDayOfMonth ) ; i ++ )
{
weekDates.append( " " + i );
}
System.out.println( weekDates );
} // end method main()
} // end class TestClass4
I used a StringBuilder object which I recommend you use, but you don't have to.