I'm learning Java, and would really appreciate any tips on how to improve this code.
It's just a simple date validation class.
import java.util.Map;
import java.util.HashMap;
class DateValidator {
private int day;
private int month;
private int year;
private enum Months {
JANUARY(1, 31), FEBRUARY(2, 28), MARCH(3, 31), APRIL(4, 30), MAY(5, 31), JUNE(6, 30),
JULY(7, 31), AUGUST(8, 31), SEPTEMBER(9, 30), OCTOBER(10, 31), NOVEMBER(11, 30), DECEMBER(12, 31);
private final int MonthNumber;
private final int numberOfDaysInMonth;
private static Map<Integer, Integer> map;
private Months(int monthMnumber, int numberOfDays) {
this.MonthNumber = monthMnumber;
this.numberOfDaysInMonth = numberOfDays;
}
static {
map = new HashMap<Integer, Integer>();
for (Months m : Months.values()) {
map.put(m.MonthNumber, m.numberOfDaysInMonth);
}
}
public static Map<Integer, Integer> getMap() {
return map;
}
}
DateValidator(int d, int m, int y) {
this.day = d;
this.month = m;
this.year = y;
}
private static boolean leapYear(int y) {
return ((y % 4 == 0) && (y % 100 != 0) || (y % 400 == 0));
}
public boolean checkDay() {
if (this.month == 2 && this.leapYear(this.year)) {
Months.getMap().put(this.month, 29);
}
return (this.day <= Months.getMap().get(this.month));
}
}
class Main {
public static void main(String[] args) {
DateValidator d = new DateValidator(31, 10, 90);
System.out.print(d.checkDay());
}
}
SimpleDateFormat
withsetLenient(false)
? \$\endgroup\$