public class Date {
int year;
int month;
int day;
/** Methode zur allgemeinen BereichsŸberprŸfung.
* Liegt der zu ŸberprŸfende Wert au§erhalb der vorgegebenen Grenzen, so wird
* die untere Grenze als Standardwert des zulŠssigen Bereichs benutzt.
*
* @param value zu ŸberprŸfender Wert
* @param lowbound Untergrenze des zulŠssigen Bereichs
* @param highbound Obergrenze des zulŠssigen Bereichs
* @param attributname im Falle eines Fehlers auszugebende Attributbezeichnung
* @return zulŠssiger Wert, ggf Standardwert
*/
protected int checkInRange(int value, int lowbound, int highbound, String attributname){
if (value < lowbound || value > highbound) {
System.out.println("Fehlerhafte Eingabe fŸr Attribut" + attributname);
System.out.println("Benutze Standardwert" + lowbound);
return lowbound;
}
return value;
}
/**
* Basic constructor accepting the day, month and year as int
*
* @param day the number of the day within the month
* @param month the number of the month, January=1, ..., December=12
* @param year the integer representing the year
*/
public Date(int day, int month, int year) {
this.day = day;
this.month = month;
this.year = year;
}
/**
* constructor accepting a Date to be copied
*
* @param date the date to be used for initialization
*/
public Date(Date date) {
day = date.day;
month = date.month;
year = date.year;
date = new Date(day, month, year);
}
/**
* sets the number of the day, month and year
*
* @param day the number of the day within the month
* @param month the number of the month, January=1, ..., December=12
* @param year the integer representing the year
*/
public void setDate(int day, int month, int year) {
setYear(year);
setMonth(month);
setDay(day);
}
/**
* sets the number of the day
*
* @param day the number of the day within the month
*/
public void setDay(int day) {
if (getMonth() == 2) {
this.day = (byte) checkInRange(day, 1, isLeapYear()? 29:28,"day");
}
else {
this.day = (byte) checkInRange(day, 1, daysInMonth[getMonth()-1], "day");
}
}
/**
* gets the number of the day
*
* @return day the number of the day within the month
*/
public int getDay() {
return day;
}
/**
* sets the number of the month
*
* @param month the number of the month, January=1, ..., December=12
*/
public void setMonth(int month) {
this.month = (byte) checkInRange (month, 1, 12, "month");
}
/**
* gets the number of the month
*
* @return month the number of the month, January=1, ..., December=12
*/
public int getMonth() {
return month;
}
/**
* sets the number of the year
*
* @param year the integer representing the year
*/
public void setYear(int year) {
this.year = year;
}
/**
* gets the current year as a number
*
* @return the integer representing the year
*/
public int getYear() {
return year;
}
/**
* tests whether current date is a valid date
*
* @return [b]true[/b] if date is valid, [b]false[/b]otherwise
*/
public boolean isValidDate() {
if ( day < 1 || day > 31 || month < 1 || month > 12 || year < 0)
return false;
if ( month == 4 || month == 6 || month == 9 || month == 11)
if ( day > 30)
return false;
if ( month == 2)
if ( day >= 30 || ( day == 29 && ! isLeapYear()))
return false;
return true;
}
/**
* calculates the number of days passed in the year
* up to, but not including, the current date
*
* @return the number of days in the year sofar and 0 for invalid dates
*/
public int passedDaysInYear(){
}
/**
* calculates the number of days remaining in the year
* not including the current date
*
* @return the number of days in the year still to go
* and 0 for invalid dates
*/
public int remainingDaysInYear(int eingabe) {
day = (day + 1) + (month - day) + (year - month); //war nur nen Gedankengang weiß das es so nicht läuft
return ((eingabe + 1) + (month - day) + (year - month)) ;
}
/**
* calculate the number of days between the date and the argument date
*
* @return the number of days between both dates
*/
public int daysBetween(Date date) {
int diff;
day = date.day;
month = date.month;
year = date.year;
diff = (day - date.day) + (month - date.month) + (year - date.year);
return diff ;
}
/**
* tests whether the year is a leap year
*
* @return [b]true[/b] if year is a leap year, [b]false[/b]otherwise
*/
public boolean isLeapYear() {
/* a year is a leap year
* - if it can be divided by 4
* - unless it is a century that cannot be divided by 400
*/
return year % 4 == 0
&& ( year % 100 != 0 || year % 400 == 0);
}
/**
* calculates the number of days in the month
*
* @return the number of days in a month
*/
public static final int [] daysInMonth = { 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
}