How to display calendar in java

Solution 1:

Can you try this example? I can see the following output:

   February 2016
   Sun  Mon Tue   Wed Thu   Fri  Sat
        1    2    3    4    5    6 
   7    8    9   10   11   12   13 
  14   15   16   17   18   19   20 
  21   22   23   24   25   26   27 
  28   29
package general;

import java.util.Scanner;

public class DisplayCalendar {

    public static void main(String[] args) {
        int Y = 2016;    // year
        int startDayOfMonth = 5;
        int spaces = startDayOfMonth;

        // startDayOfMonth

        // months[i] = name of month i
        String[] months = {
                "",                               // leave empty so that we start with months[1] = "January"
                "January", "February", "March",
                "April", "May", "June",
                "July", "August", "September",
                "October", "November", "December"
            };

            // days[i] = number of days in month i
            int[] days = {
                0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
            };

            for (int M = 1; M <= 12; M++) {

            // check for leap year
            if  ((((Y % 4 == 0) && (Y % 100 != 0)) ||  (Y % 400 == 0)) && M == 2)
                days[M] = 29;

            
            // print calendar header
            // Display the month and year
            System.out.println("          "+ months[M] + " " + Y);

            // Display the lines
            System.out.println("_____________________________________");
            System.out.println("   Sun  Mon Tue   Wed Thu   Fri  Sat");

            // spaces required
               spaces = (days[M-1] + spaces)%7;
            
            // print the calendar
            for (int i = 0; i < spaces; i++)
                System.out.print("     ");
            for (int i = 1; i <= days[M]; i++) {
                System.out.printf(" %3d ", i);
                if (((i + spaces) % 7 == 0) || (i == days[M])) System.out.println();
            }
            
            System.out.println();
        }
    }
}

Solution 2:

Since this seems to be a homework, I won't bother to give you the right algorithm. That would defeat the purpose of you - or other people with the same problem - practice your programming and analytical skills.

In this line for (int space = 1; space <= firstDayEachMonth; space++) you can totally ignore firstDayEachMonth result and use your firstDayYear counter. This counter has the starting weekday of the year and it is incremented each day that passes. Also, it is necessary to define if your week starts in 0 or 1.

In this part, you are already setting a line break for the end of the week here in:

if (firstDayYear%7 == 0)
   System.out.println();

I would reset firstDayYear when you reach this condition because since you are using it as parameter to set your spaces, you'll never have this number going greater than 7. This way you have each week line laid out correctly on the calendar and the only problem would be presenting it in the right format on the screen.

When you print the days of the week header like this System.out.println("Sun Mon Tue Wed Thu Fri Sat"); mind that you have the names with length 3 plus 5 whitespaces. So this line System.out.printf("%3d ", daysDisplay); should have a digit with a width of 3 spaces, which explains the use of %3d, plus 5 whitespaces. In this case you have 6 whitespace that you always give you the wrong offset and will cause some hell on some lines.

These are the things I've noticed and I hope it helps. Peace!