Zurzeit bearbeite ich eine Aufgabe, in der ich Tage herausfinden soll und dabei den gregorianischen Kalender benutzen. Meine Frage wäre wieso er beim ersten Datum nicht Freitag ausgibt, sondern Dienstag.
package gregorianischerKalender;
package gregorianischerKalender;
public class Test {
public static void main(String[] args) {
Datum datum = new Datum("15.10.1582");
Datum datum1 = new Datum("01.01.2000");
Datum datum2 = new Datum("22.10.2011");
Datum datum3 = new Datum("29.02.2014");
}
}
public class Datum {
private String datum;
private int day;
private int month;
private int year;
public Datum(String datum) {
this.datum = datum;
try {
day = Integer.parseInt(datum.substring(0, 2));
} catch (NumberFormatException e) {
System.out.println("No numbers!");
}
try {
month = Integer.parseInt(datum.substring(3, 5));
} catch (NumberFormatException e) {
System.out.println("No numbers!");
}
try {
year = Integer.parseInt(datum.substring(6, 10));
} catch (NumberFormatException e) {
System.out.println("No numbers!");
}
validDate();
getDay();
}
/**
* check if the year is a leapYear
* 1900 is not a leap year
* durch 4 teilbar=true, durch 100 teilbar = false,durch 400 teilbar =true
*
* @Return true if leapYear
*/
public boolean isLeapYear(int year) {
if (year % 4 == 0 && year % 100 != 0 || year % 4 == 0 && year % 400 == 0||year%400==0&&year%100==0) {
return true;
}
return false;
}
public void getDay() {
int counter = 77;
counter+= day;
int one=31,two=28,three=31,four=30,five=31,six=30,seven=31,eight=31,nine=30,ten=31,eleven=30;
String tag = "";
for (int i = 1583; i != year; i++) {
if (!isLeapYear(i)) {
counter += 365;
} else if (isLeapYear(i)) {
counter += 366;
}
}
if (!isLeapYear(year)) {
if(month==2){counter+=one;}
if(month==3){counter+=one+two;}
if(month==4){counter+=one+two+three;}
if(month==5){counter+=one+two+three+four;}
if(month==6){counter+=one+two+three+four+five;}
if(month==7){counter+=one+two+three+four+five+six;}
if(month==8){counter+=one+two+three+four+five+six+seven;}
if(month==9){counter+=one+two+three+four+five+six+seven+eight;}
if(month==10){counter+=one+two+three+four+five+six+seven+eight+nine;}
if(month==11){counter+=one+two+three+four+five+six+seven+eight+nine+ten;}
if(month==12){counter+=one+two+three+four+five+six+seven+eight+nine+ten+eleven;}
}
if (isLeapYear(year)) {
if(month==2){counter+=one;}
if(month==3){counter+=one+two+1;}
if(month==4){counter+=one+two+three+1;}
if(month==5){counter+=one+two+three+four+1;}
if(month==6){counter+=one+two+three+four+five+1;}
if(month==7){counter+=one+two+three+four+five+six+1;}
if(month==8){counter+=one+two+three+four+five+six+seven+1;}
if(month==9){counter+=one+two+three+four+five+six+seven+eight+1;}
if(month==10){counter+=one+two+three+four+five+six+seven+eight+nine+1;}
if(month==11){counter+=one+two+three+four+five+six+seven+eight+nine+ten+1;}
if(month==12){counter+=one+two+three+four+five+six+seven+eight+nine+ten+eleven+1;}
}
if (counter % 7 == 0) {
tag += "Der Tag am " + datum + " ist Freitag";
}
if (counter % 7 == 1) {
tag += "Der Tag am " + datum + " ist Samstag";
}
if (counter % 7 == 2) {
tag += "Der Tag am " + datum + " ist Sonntag";
}
if (counter % 7 == 3) {
tag += "Der Tag am " + datum + " ist Montag";
}
if (counter % 7 == 4) {
tag += "Der Tag am " + datum + " ist Dienstag";
}
if (counter % 7 == 5) {
tag += "Der Tag am " + datum + " ist Mittwoch";
}
if (counter % 7 == 6) {
tag += "Der Tag am " + datum + " ist Donnerstag";
}
System.out.println(tag);
}
public void validDate() {
if (year > 2022 || year == 2022 && month > 1 || year == 2022 && month == 1 && day > 25) {
throw new IllegalArgumentException("The current date is 25.01.2022. Your entry can't be above this date!");
}
if (year < 1582 && month < 10 && day < 15) {
throw new IllegalArgumentException("The date is under the start of the Gregorian calendar. It started with the date 15.10.1582 .");
}
if (datum.length() != 10) {
throw new IllegalArgumentException("Write the date in the format dd.MM.yyyy .");
}
if (month > 12) {
throw new IllegalArgumentException("The month can't be higher than 12.");
}
if ((month == 2 || month == 4 || month == 6 || month == 9 || month == 11) && day > 30) {
throw new IllegalArgumentException("In the months Fe,Ap,Jun,Sep and Nov can't be more than 30 days.");
}
if (day > 31) {
throw new IllegalArgumentException("There can't be more than 31 days in a month.");
}
if (datum.charAt(2) != '.' || datum.charAt(5) != '.') {
throw new IllegalArgumentException("Write the date in the format dd.MM.yyyy .");
}
for (int i = 0; i <= datum.length() - 1; i++) {
boolean b = datum.charAt(i) >= 48 && datum.charAt(i) <= 57 || datum.charAt(i) == '.';
if (!b) {
throw new IllegalArgumentException("The only thing that can be typed in with the format dd.MM.yyyy are the numbers 0-9 or a\".\".");
}//Beispiel
}
if (!isLeapYear(year) && month == 2 && day == 29) {
throw new IllegalArgumentException(+year + " is a leap year, what means that there are 29 days in february");
}
}
}
package gregorianischerKalender;
package gregorianischerKalender;
public class Test {
public static void main(String[] args) {
Datum datum = new Datum("15.10.1582");
Datum datum1 = new Datum("01.01.2000");
Datum datum2 = new Datum("22.10.2011");
Datum datum3 = new Datum("29.02.2014");
}
}
public class Datum {
private String datum;
private int day;
private int month;
private int year;
public Datum(String datum) {
this.datum = datum;
try {
day = Integer.parseInt(datum.substring(0, 2));
} catch (NumberFormatException e) {
System.out.println("No numbers!");
}
try {
month = Integer.parseInt(datum.substring(3, 5));
} catch (NumberFormatException e) {
System.out.println("No numbers!");
}
try {
year = Integer.parseInt(datum.substring(6, 10));
} catch (NumberFormatException e) {
System.out.println("No numbers!");
}
validDate();
getDay();
}
/**
* check if the year is a leapYear
* 1900 is not a leap year
* durch 4 teilbar=true, durch 100 teilbar = false,durch 400 teilbar =true
*
* @Return true if leapYear
*/
public boolean isLeapYear(int year) {
if (year % 4 == 0 && year % 100 != 0 || year % 4 == 0 && year % 400 == 0||year%400==0&&year%100==0) {
return true;
}
return false;
}
public void getDay() {
int counter = 77;
counter+= day;
int one=31,two=28,three=31,four=30,five=31,six=30,seven=31,eight=31,nine=30,ten=31,eleven=30;
String tag = "";
for (int i = 1583; i != year; i++) {
if (!isLeapYear(i)) {
counter += 365;
} else if (isLeapYear(i)) {
counter += 366;
}
}
if (!isLeapYear(year)) {
if(month==2){counter+=one;}
if(month==3){counter+=one+two;}
if(month==4){counter+=one+two+three;}
if(month==5){counter+=one+two+three+four;}
if(month==6){counter+=one+two+three+four+five;}
if(month==7){counter+=one+two+three+four+five+six;}
if(month==8){counter+=one+two+three+four+five+six+seven;}
if(month==9){counter+=one+two+three+four+five+six+seven+eight;}
if(month==10){counter+=one+two+three+four+five+six+seven+eight+nine;}
if(month==11){counter+=one+two+three+four+five+six+seven+eight+nine+ten;}
if(month==12){counter+=one+two+three+four+five+six+seven+eight+nine+ten+eleven;}
}
if (isLeapYear(year)) {
if(month==2){counter+=one;}
if(month==3){counter+=one+two+1;}
if(month==4){counter+=one+two+three+1;}
if(month==5){counter+=one+two+three+four+1;}
if(month==6){counter+=one+two+three+four+five+1;}
if(month==7){counter+=one+two+three+four+five+six+1;}
if(month==8){counter+=one+two+three+four+five+six+seven+1;}
if(month==9){counter+=one+two+three+four+five+six+seven+eight+1;}
if(month==10){counter+=one+two+three+four+five+six+seven+eight+nine+1;}
if(month==11){counter+=one+two+three+four+five+six+seven+eight+nine+ten+1;}
if(month==12){counter+=one+two+three+four+five+six+seven+eight+nine+ten+eleven+1;}
}
if (counter % 7 == 0) {
tag += "Der Tag am " + datum + " ist Freitag";
}
if (counter % 7 == 1) {
tag += "Der Tag am " + datum + " ist Samstag";
}
if (counter % 7 == 2) {
tag += "Der Tag am " + datum + " ist Sonntag";
}
if (counter % 7 == 3) {
tag += "Der Tag am " + datum + " ist Montag";
}
if (counter % 7 == 4) {
tag += "Der Tag am " + datum + " ist Dienstag";
}
if (counter % 7 == 5) {
tag += "Der Tag am " + datum + " ist Mittwoch";
}
if (counter % 7 == 6) {
tag += "Der Tag am " + datum + " ist Donnerstag";
}
System.out.println(tag);
}
public void validDate() {
if (year > 2022 || year == 2022 && month > 1 || year == 2022 && month == 1 && day > 25) {
throw new IllegalArgumentException("The current date is 25.01.2022. Your entry can't be above this date!");
}
if (year < 1582 && month < 10 && day < 15) {
throw new IllegalArgumentException("The date is under the start of the Gregorian calendar. It started with the date 15.10.1582 .");
}
if (datum.length() != 10) {
throw new IllegalArgumentException("Write the date in the format dd.MM.yyyy .");
}
if (month > 12) {
throw new IllegalArgumentException("The month can't be higher than 12.");
}
if ((month == 2 || month == 4 || month == 6 || month == 9 || month == 11) && day > 30) {
throw new IllegalArgumentException("In the months Fe,Ap,Jun,Sep and Nov can't be more than 30 days.");
}
if (day > 31) {
throw new IllegalArgumentException("There can't be more than 31 days in a month.");
}
if (datum.charAt(2) != '.' || datum.charAt(5) != '.') {
throw new IllegalArgumentException("Write the date in the format dd.MM.yyyy .");
}
for (int i = 0; i <= datum.length() - 1; i++) {
boolean b = datum.charAt(i) >= 48 && datum.charAt(i) <= 57 || datum.charAt(i) == '.';
if (!b) {
throw new IllegalArgumentException("The only thing that can be typed in with the format dd.MM.yyyy are the numbers 0-9 or a\".\".");
}//Beispiel
}
if (!isLeapYear(year) && month == 2 && day == 29) {
throw new IllegalArgumentException(+year + " is a leap year, what means that there are 29 days in february");
}
}
}