CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   Java and JSP (http://www.codingforums.com/forumdisplay.php?f=54)
-   -   What is the equation for a calendar program? (http://www.codingforums.com/showthread.php?t=277041)

cadams77 10-15-2012 03:24 PM

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...


All times are GMT +1. The time now is 03:21 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.