A few observations in no particular order:
I would consider redesigning the class so that it presents a static method:
checkDay(day, month, year). There doesn't seem to be much benefit in requiring callers to create an instance of the class, bound to a particular date. If you do this, ensure you make your constructor private so that the class cannot be instantiated.If you decide to stick with the current layout, then the fields
day,monthandyearcould be declared asfinal, since you don't adjust them at all within the class.A minor point, but your constructor may be more readable if you use
day,monthandyearrather thand,mandy.Because your
leapYearmethod is static, you should call it in a static way:public boolean checkDay() { if (this.month == 2 && leapYear(this.year)) { // <----- here Months.getMap().put(this.month, 29); }In general, you use
this.more than is necessary. I guess it's a style issue, but generally code will only usethis.when it's necessary to distinguish between local variables of the same name and class fields.In your
Monthsenum, you have a field namedMonthNumber, but it should bemonthNumberto match normal Java style.You've created an enum for your month data information, but you justhowever this is used solely to populate a map with the values and exclusively use. The rest of your code just uses the map thereafter. As a result, yourI would suggest you remove the enum is not really servingand just stick with a purpose. Ifmap, unless you intendplan to keep it private, I would consider simplifying your codeextend this class in future and just keepinguse the mapenum for other purposes. If you keepstick with using an enum, the map, it could can be declared
finalfinal.It would be easier to read your
leapYearmethod if you added some parentheses, rather than relying solely on operator precedence. You don't need the outer parentheses however:return (y % 4 == 0) && ((y % 100 != 0) || (y % 400 == 0));In your
checkDay()method, I would avoid putting data into your map. Certainly you don't want to pollute your map if you plan to move to a staticcheckDay()method as discussed above. I would do something like this instead:public boolean checkDay() { int daysInMonth = month == 2 && leapYear(year) ? 29 : Months.getMap().get(month); return day <= daysInMonth; }(Very minor...) Typically one would call your method
isLeapYearrather than simplyleapYear.