package testate; import java.applet.*; import java.awt.*; import java.awt.event.*; public class RataDieApp extends Applet { private Label headline; private TextField textField1; private TextField textField2; public void init () { headline = new Label (); textField1 = new TextField (); textField2 = new TextField (); setLayout (new BorderLayout ()); setBackground (Color.white); resize(400, 100); headline.setText ("Bestimmung des Wochentags anhand des Datums:"); add (headline, BorderLayout.NORTH); textField1.setText ("11.08.2002"); textField1.addTextListener (new TextListener () { public void textValueChanged (TextEvent evt) { textField1TextValueChanged (evt); } } ); add (textField1, BorderLayout.CENTER); textField2.setEditable (false); textField2.setText ("Bitte geben Sie oben gültiges ein Datum (TT.MM.JJJJ) ein."); add (textField2, BorderLayout.SOUTH); } public void textField1TextValueChanged (TextEvent evt) { String text=textField1.getText(); String ausgabe=""; int tag=0; int monat=0; int jahr=0; Integer temp; RataDie rd; boolean istTag=false; boolean istMonat=false; boolean istJahr=false; if ((text.compareTo("")!=0)&&(text.length()<11)) { try { temp=Integer.valueOf(text.substring(0,2)); tag=temp.intValue(); istTag=true; } catch (NumberFormatException e) {} catch (Exception e) {} try { temp=Integer.valueOf(text.substring(3,5)); monat=temp.intValue(); istMonat=true; } catch (NumberFormatException e) {} catch (Exception e) {} try { temp=Integer.valueOf(text.substring(6,text.length())); jahr=temp.intValue(); istJahr=true; } catch (NumberFormatException e) {} catch (Exception e) {} if ((istTag)&&(istMonat)&&(istJahr)) { rd = new RataDie(tag, monat, jahr); ausgabe=rd.getDayName()+", "+rd; } else { ausgabe="Es wurde kein korrektes Datum eingegeben!"; } textField2.setText(ausgabe); } } } class RataDie extends java.lang.Object { private int year = 0; private int month = 0; private int day = 0; /** erzeugt ein Objekt vom Typ RataDie */ public RataDie (int day, int month, int year) { boolean correctDate = false; // kann das Datum ueberhaupt sein??? if ((month>0)&&(month<13)&&(year>0)) { if ((day>0)&&(day<32)) { if (month==2) { if (istSchaltjahr(year)&&(day<30)) correctDate=true; else if (day<29) correctDate=true; } else if ((month==4)&&(day<31)) { correctDate=true; } else if ((month==6)&&(day<31)) { correctDate=true; } else if ((month==9)&&(day<31)) { correctDate=true; } else if ((month==11)&&(day<31)) { correctDate=true; } else if ((month==1)||(month==3)||(month==5)||(month==7)||(month==8)||(month==10)||(month==12)) { correctDate=true; } } } // wenn ja, weise Werte zu; wenn nein, nimm 01.01.0001 als Datum if (correctDate) { this.year = year; this.month = month; this.day = day; } else { this.year = 1; this.month = 1; this.day = 1; } } /** prueft, ob das angegebene Jahr ein Schaltjahr ist */ private boolean istSchaltjahr(int year) { boolean out = false; if ((((year%4)==0)&&((year%100)!=0))||((year%400)==0)) { out = true; } return out; } /** bestimmt die GAUSS'sche Klammer einer Zahl */ public long GAUSS(double zahl) { long out = (long) zahl; if (zahl<0.0) { out=out-1; } return out; } public long RD() { long Y = year-1; // Anzahl der Tage ausser den Schalttagen der vorangegangenen Jahren: long a = 365*Y; // Anzahl der Schalttage in den vorangegangenen Jahren: long b = GAUSS(Y*0.25)-GAUSS(Y*0.01)+GAUSS(Y*0.0025); // Anzahl der Tage in den Monaten der Jahres vor dem Monat: long c = GAUSS((367*month-362)/12); long d = 0; // Anzahl der Tage implements aktuellem Monat: long e = day; // Fehlerkorrektur fuer c (Februar wurde mit 30 Tagen gezaehlt): if ((month>2)&&istSchaltjahr(year)) d=-1; if ((month>2)&&!istSchaltjahr(year)) d=-2; // gib die Summe der Tage zurueck return (a+b+c+d+e); } /** gibt die Nummer des Wochentages aus (0 = Sonntag, 1 = Montag, ... , 6 = Freitag) */ public int getDayNumber() { return ((int) (this.RD()%7)); } /** gibt den (deutschen) Wochentag aus */ public String getDayName() { int tag = this.getDayNumber(); if (tag==1) { return "Montag"; } if (tag==2) { return "Dienstag"; } if (tag==3) { return "Mittwoch"; } if (tag==4) { return "Donnerstag"; } if (tag==5) { return "Freitag"; } if (tag==6) { return "Samstag"; } if (tag==0) { return "Sonntag"; } return "geht gar nicht!"; } /** (Standard) liefert ein String-Objekt zureuck */ public String toString() { String out; if (day<10) out = "0"+day+"."; else out = day+"."; if (month<10) out = out+"0"+month+"."; else out = out+month+"."; if (year<10) out = out+"000"+year; else if (year<100) out = out+"00"+year; else if (year<1000) out = out+"0"+year; else out = out+year; return out; } }