The obvious
As mentioned in @mtj's comment, we have three different JDK APIs for doing this, so you really shouldn't have to resort to reinventing-the-wheel.
java.util.Date.after(Date)
java.util.Calendar.after(Object)
java.time.LocalDate.isAfter(ChronoLocalDate)
Naming
Ok, maybe this is some simple assignment that you need to do... how about the naming? You should try to avoid using the same class name from the JDK, as it will get messy understanding which class is actually being used. For example, the wrong usage of your class might lead to confusing toString()
representation, or syntax errors when trying to do a Date.getTime()
.
Also, since you are already using English mostly, you should translate that French-sounding method name to the English equivalent too, e.g. isAfter()
. Whether you should have your entire codebase in non-English is another debating topic altogether. :)
Validation
You should be checking whether day
and month
are valid or not... you wouldn't want your bespoke class to start representing the 99th day of the 555th month of the 18288th year... assuming we're still talking about the Greogorian calendar.
null
check
Next, assuming you have renamed this class, you should consider handling null
properly: do you want to throw a NullPointerException
as it is currently, or return a default value?
Boolean expressions
Finally, you can chain the boolean expressions together using &&
and ||
so that the method returns true
the moment any of the expression is true
too:
return dateToCompare != null && // assuming returning false for null values
year > dateToCompare.getYear() || (year == dateToCompare.getYear() &&
(month > dateToCompare.getMonth() || (month == dateToCompare.getMonth() &&
day > dateToCompare.getDay())));
Admittedly, this is getting hard to comprehend.
(thanks to @JS1's answer for the update)