Interactive
Run on Kotlin Playground
Source code
import java.time.DayOfWeek
import java.time.LocalDate
import java.time.format.DateTimeFormatter
private val dateFormatter = DateTimeFormatter.ofPattern("uuuu-MM-dd, EEEE")
private val weekend = listOf(DayOfWeek.SATURDAY, DayOfWeek.SUNDAY)
private const val DAY_IN_MONTH = "2022-04-01"
fun main() {
val randomDayInMonth = LocalDate.parse(DAY_IN_MONTH)
daysList(randomDayInMonth)
}
fun daysList(randomDayInMonth: LocalDate) {
val start = randomDayInMonth.withDayOfMonth(1)
val end = start.plusMonths(1)
var numberOfWorkedDays = 0
start.datesUntil(end).forEach {
if (it.dayOfWeek in weekend) {
println()
return@forEach
}
val dateFormatted = it.format(dateFormatter).padEnd(21, ' ')
println("$dateFormatted 8 hours")
numberOfWorkedDays++
}
println(
"""
Total worked days: $numberOfWorkedDays
Total worked hours: ${numberOfWorkedDays * 8}
""".trimIndent()
)
}