Quellcode
- /**
- *
- * @author Torben Brodt
- * @version 1.0
- *
- * <p />Kalender
- * <p />Funktioniert mit Java < 1.5
- */
- public class A2_Kalender {
- /**
- * @param args
- */
- public static void main(String[] args) {
- java.util.Scanner input = new java.util.Scanner(System.in);
- System.out.print("Bitte geben Sie den Monat des Datums ein: ");
- int m = input.nextInt();
- System.out.print("Bitte geben Sie den Jahr des Datums ein: ");
- int y = input.nextInt();
- char w[] = {'M','D','M','D','F','S','S'};
- for(char day : w)
- System.out.print(" "+day);
- System.out.println();
- int tag = alteWochentagFunktion(1,m,y);
- int ende = tageImMonat(m,y);
- String ausgabe="";
- int tage=0, i=1;
- System.out.print(" ");
- while(tage < ende)
- {
- if(i > tag)
- {
- tage++;
- ausgabe = (i % 7 == 0) ? tage+"
- " : tage+" ";
- if(y == 1582 && m == 10 && tage > 4 && tage < 15)
- ausgabe = "false";
- } else
- ausgabe = " ";
- if(ausgabe != "false")
- {
- i++;
- System.out.print(ausgabe);
- }
- }
- }
- static int alteWochentagFunktion(int d, int m, int y) {
- int tage=0, id=1, im=1, iy=1;
- // Anzahl der Tage im Monat
- int monate[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
- monate[2] = isSchaltjahr(y) ? 29 : monate[2];
- //Jahr angleichen
- while(iy<y)
- { tage += isSchaltjahr(iy) ? 366 : 365;
- iy++;
- }
- //Monat angleichen
- while(im<m)
- { tage += monate[im];
- im++;
- }
- //Tag angleichen
- while(id<d)
- { tage += 1;
- id++;
- }
- // gestrichene tage abziehen
- tage += (y == 1582 && (m == 10 && d > 15) || (m > 10)) || y > 1582 ? 11 : 0;
- //Samstag = 1. jan 1
- int wochentage[] = {5,6,0,1,2,3,4};
- return wochentage[tage % 7];
- }
- static int tageImMonat(int m, int y) {
- int monate[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
- monate[2] = isSchaltjahr(y) ? 29 : monate[2];
- return monate[m];
- }
- static boolean isSchaltjahr(int y)
- {
- if((y % 4) == 0 && (y < 1600))
- return true;
- else
- return (y % 4 == 0) && (!(y % 100 == 0) || (y % 400 == 0)) ? true : false;
- }
- }