1. Overview
A leap year contains an additional day – 366 days instead of 365 days. So, while working with date and time systems and use cases, it’s essential to account for the extra day.
In this tutorial, we’ll learn different ways to check leap year in Kotlin.
2. Algorithm
To check if a year is a leap year, we can check if the year is evenly divisible by 4. Further, for a century year, we also need to check it’s evenly divisible by 400.
Using this approach, we can say that 2024 is a leap year because it’s evenly divisible by 4. Similarly, 1900 isn’t a leap year because it’s not evenly divisible by 400 despite being divisible by 4.
Now that we understand the algorithm, we’ll implement it using different programming constructs available in Kotlin.
3. Using If–Else
Our algorithm is based on checking a few conditions, so let’s start by implementing it using the if–else construct.
For this purpose, we’ll write the isLeapYearUsingIfElse() function:
fun isLeapYearUsingIfElse(year: Int): Boolean {
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
return true
} else {
return false
}
}
The implementation is quite intuitive. We check for the arithmetic conditions for leap year and return true when the condition is met. Otherwise, we return false.
Now, let’s verify the isLeapYearUsingIfElse() function for a few leap years:
Assertions.assertTrue(isLeapYearUsingIfElse(2024))
Assertions.assertTrue(isLeapYearUsingIfElse(2000))
Similarly, let’s check a few negative scenarios where our function should return false:
Assertions.assertFalse(isLeapYearUsingIfElse(2023))
Assertions.assertFalse(isLeapYearUsingIfElse(1900))
Perfect! It looks like we’ve got this one right.
4. Using Single Expression
We can further simplify our implementation by having a single expression in the return statement:
fun isLeapYearUsingSingleExpression(year: Int): Boolean {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)
}
Like always, we must check the outcome for different scenarios:
Assertions.assertTrue(isLeapYearUsingSingleExpression(2024))
Assertions.assertTrue(isLeapYearUsingSingleExpression(2000))
Assertions.assertFalse(isLeapYearUsingSingleExpression(2023))
Assertions.assertFalse(isLeapYearUsingSingleExpression(1900))
It works!
5. Using when Expression
In Kotlin, we can make our conditional statements more readable by using when expressions. Let’s see how we can use this idiom to write the isLeapYearUsingWhenExpression() function that uses a when expression:
fun isLeapYearUsingWhenExpression(year: Int): Boolean {
when {
(year % 4 == 0 && year % 100 != 0) || (year % 400 == 0) -> return true
else -> return false
}
}
We can see that the condition remains the same, but our code is much more concise.
Next, let’s verify it for both positive and negative scenarios:
Assertions.assertTrue(isLeapYearUsingWhenExpression(2024))
Assertions.assertTrue(isLeapYearUsingWhenExpression(2000))
Assertions.assertFalse(isLeapYearUsingWhenExpression(2023))
Assertions.assertFalse(isLeapYearUsingWhenExpression(1900))
The results are correct.
6. Using Extension Function
We can also write an extension function for the Calendar class that checks whether a calendar year is a leap year:
fun Calendar.isLeapYearCalendar(): Boolean {
val daysInYear = this.getActualMaximum(Calendar.DAY_OF_YEAR)
return daysInYear > 365
}
It’s worth noting that we call the getActualMaximum() function with Calendar.DAY_OF_YEAR property to get the number of days in the calendar year. Further, we can directly call the isLeapYearCalendar() function for Calendar values.
So, let’s go ahead and verify it for both scenarios:
val calendar = Calendar.getInstance()
calendar.set(Calendar.YEAR, 2024)
Assertions.assertTrue(calendar.isLeapYearCalendar())
calendar.set(Calendar.YEAR, 2000)
Assertions.assertTrue(calendar.isLeapYearCalendar())
calendar.set(Calendar.YEAR, 2023)
Assertions.assertFalse(calendar.isLeapYearCalendar())
calendar.set(Calendar.YEAR, 1900)
Assertions.assertFalse(calendar.isLeapYearCalendar())
Great! It looks like we nailed this one.
7. Conclusion
In this article, we learned how to check leap year in Kotlin. Furthermore, we explored concepts like when expression, if–else construct, and extension function while solving the use case.
As always, the code from this article is available over on GitHub.