Subato

Date (Folgetag, Kalender)

class Date {
  int day;
  int month;
  int year;
  
  Date(int day, int month, int year) {
    assert month > 0;
    assert month < 13;
    assert day > 0;
    assert day < 32;
  
    this.day = day;
    this.month = month;
    this.year = year;
  }
  
  Date nextDay() {
    if (month == 12 && day == 31)
      return new Date(1, 1, year + 1);
    if (day==daysInMonth())
      return new Date(1,month+1,year);
    return new Date(day + 1, month, year);
  }
  
  int daysInMonth() {
    if (month == 2)
      return isLeapYear() ? 29 : 28;
    switch (month) {
      case 4:
      case 6:
      case 9:
      case 11:
        return 30;
      default:
        return 31;
    }
  }
  
  String monthAsHTML() {
    String result = "<table><tr><th>Mo</th><th>Di</th><th>Mi</th>"
        +"<th>Do</th><th>Fr</th><th>Sb</th><th>So</th></tr>";
    int wd = new Date(1, month, year).dayOfWeek();
    if (wd == 0)
      wd = 7;
    result=result+"<tr>";
    for (int i = 1; i < wd; i++) {
      result += "<td></td>";
    }
    int tds  = wd-1;
    for (int i=1;i<=daysInMonth();i++){
      if (tds%7==0&&tds>0) result += "</tr><tr>";
      result+="<td>"+i+"</td>";
      tds++;
    }
    result += "</tr>";
    return result + "</table>";
  }
 
  public String toString() {
    String[] wds = {"So", "Mo", "Di", "Mi", "Do", "Fr", "Sb"};
    return wds[dayOfWeek()] + " " + day + "." + month + "." + year;
  }

  int dayOfWeek(){
    //Schrittweise Lösung über die Einzelziffern
    int tz = day%7;
    int[] monatsziffern = {0,3,3,6,1,4,6,2,5,0,3,5};
    int mz = monatsziffern[month-1];
    int jz = year%100; 
    int jahresziffer = (jz+jz/4)%7;
    int jh = year/100;
    int jahrhundertziffer = (3 - jh%4)*2;
    int schaltjahreskorrektur
        = ((month<=2)&&isLeapYear())?6:0;
    return (tz+mz+jahresziffer+jahrhundertziffer+schaltjahreskorrektur)%7;
  }
  
  boolean isLeapYear() {
    return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
  }
  
  int getAbsoluteDaysInYear() {
    return isLeapYear() ? 366 : 365;
  }
  
  public static void main(String[] args) {
    Date d = new Date(30, 11, 2018);
    System.out.println(d);
    System.out.println(d.monthAsHTML());
  }
}