Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1
  1. #1
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    What is the equation for a calendar program?

    I am creating a program that prompts the user to enter a year and the first day of the month, Sun=0, Mon=1, etc . . . then shows that years calendar with the correct number date placement, but I am having severe trouble getting the actual numbered dates up and changing for every single month... Honestly I am just not quite sure how to do it. The calendars need to be displayed in the console. Any help is appreciated... I'm just lost.

    Here's what I have so far:

    Code:
     
    import javax.swing.JOptionPane;
    
    public class test {
    
        public static void main(String[] args) {
    
    
    
    
           // Prompt the user to enter input
        String yearString = JOptionPane.showInputDialog("Enter a year:");
        int year = Integer.parseInt(yearString);
    
        String firstDayString = JOptionPane.showInputDialog("Enter the first day of the year:\n Sun=0, Mon=1, Tues=2, etc...");
        int firstDay = Integer.parseInt(firstDayString);
    
        int numberOfDaysInMonth = 0;
    
        
        
        // Display calendar for each month
        for (int month = 1; month <= 12; month++) {
            
         
            // Display Calendar title
          switch (month) {
            case 1: System.out.printf("                        January\n=======================================================\nSun     Mon     Tues     Wed      Thurs     Fri     Sat\n");
    
                    numberOfDaysInMonth = 31;
                    firstDay = (firstDay + numberOfDaysInMonth) % 7;
                    break;
            case 2: System.out.printf("                        February\n=======================================================\nSun     Mon     Tues     Wed      Thurs     Fri     Sat\n");
                    if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
                      numberOfDaysInMonth = 29;
                    else
                      numberOfDaysInMonth = 28;
                    firstDay = (firstDay + numberOfDaysInMonth) % 7;
            break;
            case 3: System.out.printf("                        March\n=======================================================\nSun     Mon     Tues     Wed      Thurs     Fri     Sat\n");
            numberOfDaysInMonth = 31;
            firstDay = (firstDay + numberOfDaysInMonth) % 7;
            break;
            case 4: System.out.printf("                        April\n=======================================================\nSun     Mon     Tues     Wed      Thurs     Fri     Sat\n");
            numberOfDaysInMonth = 30;
            firstDay = (firstDay + numberOfDaysInMonth) % 7;
            break;
            case 5:System.out.printf("                        May\n=======================================================\nSun     Mon     Tues     Wed      Thurs     Fri     Sat\n");
            numberOfDaysInMonth = 31;
            firstDay = (firstDay + numberOfDaysInMonth) % 7;
            break;
            case 6: System.out.printf("                        June\n=======================================================\nSun     Mon     Tues     Wed      Thurs     Fri     Sat\n");
            numberOfDaysInMonth = 30;
            firstDay = (firstDay + numberOfDaysInMonth) % 7;
            break;
            case 7: System.out.printf("                        July\n=======================================================\nSun     Mon     Tues     Wed      Thurs     Fri     Sat\n");
            numberOfDaysInMonth = 31;
            firstDay = (firstDay + numberOfDaysInMonth) % 7;
            break;
            case 8: System.out.printf("                        August\n=======================================================\nSun     Mon     Tues     Wed      Thurs     Fri     Sat\n");
            numberOfDaysInMonth = 31;
            firstDay = (firstDay + numberOfDaysInMonth) % 7;
            break;
            case 9: System.out.printf("                        September\n=======================================================\nSun     Mon     Tues     Wed      Thurs     Fri     Sat\n");
            numberOfDaysInMonth = 30;
            firstDay = (firstDay + numberOfDaysInMonth) % 7;
            break;
            case 10: System.out.printf("                        October\n=======================================================\nSun     Mon     Tues     Wed      Thurs     Fri     Sat\n");
            numberOfDaysInMonth = 31;
            firstDay = (firstDay + numberOfDaysInMonth) % 7;
            break;
            case 11: System.out.printf("                        November\n=======================================================\nSun     Mon     Tues     Wed      Thurs     Fri     Sat\n");
            numberOfDaysInMonth = 30;
            firstDay = (firstDay + numberOfDaysInMonth) % 7;
            break;
            case 12: System.out.printf("                        December\n=======================================================\nSun     Mon     Tues     Wed      Thurs     Fri     Sat\n");
            numberOfDaysInMonth = 31;
            firstDay = (firstDay + numberOfDaysInMonth) % 7;
            break;
          }
    
    //      //firstday switch
    //      switch(firstDay){
    //          case 0:  for(int num1 = 1; num1 <= 7; num1++){
    //        System.out.print(num1 + "        ");
    //          }
    //              break;
    //          case 1:
    //              for(int num1 = 1; num1 <= 6; num1++){
    //        System.out.print(num1 + "        ");
    //          }
    //              break;
    //          case 2:
    //              for(int num1 = 1; num1 <= 5; num1++){
    //        System.out.print(num1 + "        ");
    //          }
    //              break;
    //          case 3:
    //              for(int num1 = 1; num1 <= 4; num1++){
    //        System.out.print(num1 + "        ");
    //          }
    //              break;
    //          case 4:
    //              for(int num1 = 1; num1 <= 3; num1++){
    //        System.out.print(num1 + "        ");
    //          }
    //              break;
    //          case 5:
    //              for(int num1 = 1; num1 <= 2; num1++){
    //        System.out.print(num1 + "        ");
    //          }
    //              break;
    //          case 6:
    //              for(int num1 = 1; num1 <= 1; num1++){
    //        System.out.print(num1 + "        ");
    //          }
    //              break;
    //      }
          
          //dates
       for(int num1 = 1; num1 <= 7; num1++){
            System.out.print(num1 + "        ");
              }
         System.out.println("\n");
        for(int num2 = 8; num2 <= 14; num2++){
        System.out.print(num2+"       ");
        }
        System.out.println("\n");
        for(int num3 = 15; num3 <= 21; num3++){
            System.out.print(num3+"       ");
        }
         System.out.println("\n");
        for(int num4 = 22; num4 <= 28; num4++){
            System.out.print(num4+"       ");
        }
         System.out.println("\n");
        for(int num5 = 29; num5 <= 31; num5++){
            System.out.print(num5+"       ");
        }
        System.out.println("\n");
    
    //      // Get the start day for the next month
    
        }
      }
    }
    I just have no idea what formula to use to get the dates to switch spaces for every month without using like 500 lines...
    Last edited by cadams77; 10-15-2012 at 04:28 PM.


 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •