Date helpers
- Kotlin course
- Intermediate
- Lesson 22/116
- 6 min
Add LocalDate processing functions.
LocalDate extension
This lesson adds the isWeekend extension to LocalDate. It returns whether the date is on a weekend.
Example: fun LocalDate.isWeekend(): Boolean { val day = dayOfWeek.value; return day == 6 || day == 7 }. Using LocalDate requires import java.time.LocalDate. dayOfWeek.value returns 1 for Monday and 7 for Sunday. Saturday 6 and Sunday 7 are the weekend. LocalDate.of(2024, 1, 6).isWeekend() returns true.
dayOfWeek and the weekend
The dayOfWeek property of LocalDate gives the day of the week. With .value you get its number: Monday 1, Sunday 7.
Example: LocalDate.of(2024, 1, 6).dayOfWeek.value returns 6 because January 6, 2024 is a Saturday. For a weekend check use day == 6 || day == 7. This pattern collects the weekend logic in one extension function instead of rewriting it on every call.
Practice Add the LocalDate.isWeekend extension
Alongside the existing Helpers object, String.titleCase and Int.squared extensions, add a LocalDate.isWeekend() extension. First put import java.time.LocalDate at the top of the file. The extension body runs val day = dayOfWeek.value; return day == 6 || day == 7. In main print Helpers.greet("Alex"), "hello world".titleCase(), 5.squared() and LocalDate.of(2024, 1, 6).isWeekend() with println.
Starter code
object Helpers {
fun greet(name: String): String {
return "Hello, $name!"
}
}
fun String.titleCase(): String {
return split(" ").joinToString(" ") { word ->
word.lowercase().replaceFirstChar { it.uppercase() }
}
}
fun Int.squared(): Int {
return this * this
}
// add the LocalDate import at the top
// LocalDate extension: isWeekend
// Saturday (6) or Sunday (7)?
// call all helpers in mainSample solution
import java.time.LocalDate
object Helpers {
fun greet(name: String): String {
return "Hello, $name!"
}
}
fun String.titleCase(): String {
return split(" ").joinToString(" ") { word ->
word.lowercase().replaceFirstChar { it.uppercase() }
}
}
fun Int.squared(): Int {
return this * this
}
fun LocalDate.isWeekend(): Boolean {
val day = dayOfWeek.value
return day == 6 || day == 7
}
fun main() {
println(Helpers.greet("Alex"))
println("hello world".titleCase())
println(5.squared())
println(LocalDate.of(2024, 1, 6).isWeekend())
}Expected output
Hello, Alex! Hello World 25 true
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Complete the LocalDate extension definition
fun ____.isWeekend(): Boolean {
val day = dayOfWeek.value
return day == 6 || day == 7
}
fun LocalDate.isWeekend(): Boolean {
val day = dayOfWeek.value
return day == 6 || day == 7
}
Explanation: LocalDate is the receiver type for the isWeekend extension.
2 Match the LocalDate extension parts
| Term | Meaning |
|---|---|
import java.time.LocalDate |
Import |
fun LocalDate.isWeekend() |
Receiver and method |
dayOfWeek.value |
Day number |
day == 6 || day == 7 |
Weekend condition |
3 What does the <code>fun LocalDate.isWeekend(): Boolean</code> extension return?
- A String
- An Int
- A Boolean Correct answer
- A LocalDate
Explanation: isWeekend returns a Boolean; whether it is the weekend.