import java.time.*;
class Leap {
boolean isLeapYear(int year) {
return LocalDate.of(year, Month.FEBRUARY, 28).plusDays(1).getDayOfMonth() == 29;
}
}
This approach may be considered a "cheat" for this exercise.
By adding a day to February 28th for the year, you can see if the new day is the 29th or the 1st.
If it is the 29th, then the year is a leap year.
This is done by using the plusDays()
and getDayOfMonth()
methods of the LocalDate class.
27th Nov 2024
·
Found it useful?