javaloopsinputcalendar

Display calendar month based off of the year and day the year starts (Java)


I am writing a code that takes the user input year and month and prints out a calendar of the specific month based off of the given month. It uses loops to make the printout possible.

This is the desired output

Enter a year: 2013
Enter a month (1-12): 2

                 February 2013
    ----------------------------------------
    sun  mon  tue   wed  thrus   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   

The thing with my code that I looked online on how to print an entire calendar (Jan to Dec full year) but I was wondering if there is a way to shorten the entire printout to just a selected month from the user. I did look at this question as a reference and found it quite helpful but the answers all print the entire year. How to display calendar in java

What I believe should be changed about the program is that for the months there should be for loops and parts of the code determining if the year is a leap year or not. I think there should be an array for the selection of months and whichever one that is there will print out in the middle of the section.


Solution

  • Run this and tell me if it does what you are looking for:

    import java.util.Scanner;
    
    public class Test {
    
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            System.out.print("Enter the year: ");
            int Y = sc.nextInt(); // year
            System.out.println();
            System.out.print("Enter the weekday the year starts on (1 for Sunday, 2 for Monday, 2 for Tuesday, etc:) ");
            int startDayOfMonth = sc.nextInt();
            int spaces = startDayOfMonth-1;
            System.out.println();
    
            // 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 };
            
            System.out.print("Enter the month you wish to display (1 for January, 2 for February, etc) ");
            int M = sc.nextInt();
            System.out.println();
            // 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();
        }
    }
    

    I edited the answer from the post you referenced to utilize a scanner that chooses a specific month rather than going through all of the months