Created
February 28, 2023 07:52
-
-
Save prongbang/9869ec50bf3f30dee1aa594b16d87a33 to your computer and use it in GitHub Desktop.
Get date list between start - end date
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.time.DayOfWeek | |
import java.time.LocalDate | |
data class Holiday( | |
val date: LocalDate, | |
val userId: Int, | |
) { | |
companion object { | |
val all = listOf( | |
Holiday(date = LocalDate.of(2023, 2, 6), userId = 1), | |
) | |
} | |
} | |
data class LeavePlan( | |
val date: LocalDate, | |
val userId: Int, | |
) { | |
companion object { | |
val all = listOf( | |
LeavePlan(date = LocalDate.of(2023, 2, 1), userId = 1), | |
LeavePlan(date = LocalDate.of(2023, 2, 2), userId = 1), | |
) | |
} | |
} | |
fun calculateDateList(start: LocalDate, end: LocalDate): List<LocalDate> { | |
var date = start | |
val dateList = mutableListOf<LocalDate>() | |
while (!date.isAfter(end)) { | |
dateList.add(date) | |
date = date.plusDays(1) | |
} | |
return dateList | |
} | |
fun isWeekday(date: LocalDate): Boolean { | |
val dayOfWeek = date.dayOfWeek | |
return dayOfWeek != DayOfWeek.SATURDAY && dayOfWeek != DayOfWeek.SUNDAY | |
} | |
fun main(args: Array<String>) { | |
val userId = 1 | |
val isSale = false | |
val start = LocalDate.of(2023, 1, 31) | |
val end = LocalDate.of(2023, 2, 7) | |
val dateList = calculateDateList(start, end) | |
dateList.forEach { local -> | |
val isHoliday = Holiday.all.indexOfFirst { it.date.compareTo(local) == 0 && it.userId == userId } > -1 | |
val isLeavePlan = LeavePlan.all.indexOfFirst { it.date.compareTo(local) == 0 && it.userId == userId } > -1 | |
// Check sale | |
var weekday = true | |
if (isSale.not()) { | |
weekday = isWeekday(local) | |
} | |
if (isHoliday.not() && isLeavePlan.not() && weekday) { | |
println(local) | |
} | |
// Result: | |
// 2023-01-31 | |
// 2023-02-03 | |
// 2023-02-07 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment