123 lines
3.0 KiB
Java
123 lines
3.0 KiB
Java
package fr.istic.vv;
|
|
|
|
class Date implements Comparable<Date> {
|
|
private int day;
|
|
private int month;
|
|
private int year;
|
|
|
|
public Date(int day, int month, int year) {
|
|
if (isValidDate(day, month, year)) {
|
|
this.day = day;
|
|
this.month = month;
|
|
this.year = year;
|
|
} else {
|
|
throw new IllegalArgumentException("Invalid date");
|
|
}
|
|
}
|
|
|
|
public static boolean isValidDate(int day, int month, int year) {
|
|
if (month < 1 || month > 12 || day < 1 || day > 31)
|
|
return false;
|
|
int max_day = max_day(month, year);
|
|
if (day > max_day)
|
|
return false;
|
|
return true;
|
|
}
|
|
|
|
public static boolean isLeapYear(int year) {
|
|
return year % 4 == 0;
|
|
}
|
|
|
|
public Date nextDate() {
|
|
int d = this.getDay();
|
|
int m = this.getMonth();
|
|
int y = this.getYear();
|
|
int max_day = max_day(m, y);
|
|
if (d < max_day) {
|
|
return new Date(d + 1, m, y);
|
|
} else {
|
|
if (m == 12) {
|
|
return new Date(1, 1, y + 1);
|
|
} else {
|
|
return new Date(1, m + 1, y);
|
|
}
|
|
}
|
|
}
|
|
|
|
public Date previousDate() {
|
|
int d = this.getDay();
|
|
int m = this.getMonth();
|
|
int y = this.getYear();
|
|
if (d == 1) {
|
|
if (m == 1) {
|
|
return new Date(31, 12, y - 1);
|
|
} else {
|
|
return new Date(max_day(m - 1, y), m - 1, y);
|
|
}
|
|
} else {
|
|
return new Date(d - 1, m, y);
|
|
}
|
|
}
|
|
|
|
public int compareTo(Date other) {
|
|
if (other == null) {
|
|
throw new NullPointerException();
|
|
}
|
|
int cmpYear = Integer.compare(this.getYear(), other.getYear());
|
|
if (cmpYear != 0)
|
|
return cmpYear;
|
|
|
|
int cmpMonth = Integer.compare(this.getMonth(), other.getMonth());
|
|
if (cmpMonth != 0)
|
|
return cmpMonth;
|
|
|
|
return Integer.compare(this.getDay(), other.getDay());
|
|
|
|
}
|
|
|
|
public int getDay() {
|
|
return day;
|
|
}
|
|
|
|
public int getMonth() {
|
|
return month;
|
|
}
|
|
|
|
public int getYear() {
|
|
return year;
|
|
}
|
|
|
|
private static int max_day(int month, int year) {
|
|
int max_verstappen;
|
|
switch (month) {
|
|
case 1:
|
|
case 3:
|
|
case 5:
|
|
case 7:
|
|
case 8:
|
|
case 10:
|
|
case 12:
|
|
max_verstappen = 31;
|
|
break;
|
|
case 4:
|
|
case 6:
|
|
case 9:
|
|
case 11:
|
|
max_verstappen = 30;
|
|
break;
|
|
case 2: {
|
|
if (isLeapYear(year)) {
|
|
max_verstappen = 29;
|
|
break;
|
|
} else {
|
|
max_verstappen = 28;
|
|
break;
|
|
}
|
|
}
|
|
default:
|
|
max_verstappen = 0;
|
|
break;
|
|
}
|
|
return max_verstappen;
|
|
}
|
|
} |