I've been working on a project and in my private static class MyDateEvaluator , there's a deprecated api and that is getDate().
This is my first code:
@Override
public boolean isInvalid(Date date) {
return date.getDay() == 0;
}
Then I tried to change it to Calendar.get(Calendar.DAY_OF_MONTH), but it won't work and I get an error message,
MyDateEvaluator is not abstract.
public boolean isInvalid(Calendar Calendar) {
return 0 == Calendar.get(Calendar.DAY_OF_MONTH);
}
java.time
I suggest:
It would be still better if you could avoid the old-fashioned
Dateclass completely. I have assumed that the method signature is in an interface or superclass that you cannot change. If so, the parameter does need to have typeDate, we cannot substitute withLocalDate(nor withCalendaras you tried).Both
DateandCalendarare poorly designed and long outdated. So the best thing we can do when we get aDateis convert it to anInstantand perform any further conversions from there. My code depends on the JVM’s default time zone, which is shaky because the setting can be changed at any time. However, given the poor design of theDateclass there is no way we can avoid this.An option that is a bit simpler than the above and may be good enough would be to declare
DayOfWeek invalidDay = DayOfWeek.SUNDAY;instead of the set and compare using.equals.