LocalDate plusDays

Leap
Leap in Scala
import java.time.{LocalDate, Month}

object Leap {
  def leapYear(year: Int): Boolean =
    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.

9th Oct 2024 · Found it useful?