What is currently the most idiomatic way to check ...
# getting-started
l
What is currently the most idiomatic way to check if a date (today) is between a range of dates, e.g. Nov 28th and Jan 6th (regardless of the year)?
👍 1
Do I still need to resort to Java
LocalDate
? Or is there a class for this in Kotlin stdlib?
I now came up with
Copy code
// Nov 28th - Jan 6th (both inclusive and regardless of year)
        val isXmasSeason = with(LocalDate.now()) {
            (month == Month.NOVEMBER && dayOfMonth >= 28) ||
                    month == Month.DECEMBER ||
                    (month == Month.JANUARY && dayOfMonth <= 6)
        }
I guess if it's regardless of year then there's no nicer way.
b
Most idiomatic would be to use https://github.com/Kotlin/kotlinx-datetime
➕ 2
l
Thanks!
Although I guess the logic won't change in this case
j
Pseudo code but with Kotlinx date time I think it was possible to do:
Copy code
val isBetween = date in dateA..dateB
s
@Javier that would take the year into account which is not wanted.
👌 1
j
I don't tend to use kotlinx datetime, but probably the thing you want is a MonthDay? no tests here, but something like:
Copy code
import java.time.LocalDate
import java.time.MonthDay

fun LocalDate.between(a: MonthDay, b: MonthDay): Boolean {
    val md = MonthDay.from(this)
    
    if ( a < b ) {
        return a <= md && md <= b        
    }
    else {
        return a <= md || b >= md
    }
}

print(LocalDate.of(2025, 11, 14).between(MonthDay.of(11, 6), MonthDay.of(1, 9)))
print(LocalDate.of(2025, 12, 14).between(MonthDay.of(11, 6), MonthDay.of(1, 9)))
print(LocalDate.of(2025, 1, 4).between(MonthDay.of(11, 6), MonthDay.of(1, 9)))
print(LocalDate.of(2025, 1, 10).between(MonthDay.of(11, 6), MonthDay.of(1, 9)))
print(LocalDate.of(2025, 11,5).between(MonthDay.of(11, 6), MonthDay.of(1, 9)))
thank you color 1
LocalDate could probably be some wider Temporal type tbh
s
I didn't know that MonthDay exists 😅 Looks good to me 👍 (though you should probably move the return infront of the if-else expression)
🎉 1
j
alt-enter left as an exercise for the reader 🤣
s
clearly the code golfing part of the thread has begun. How about:
Copy code
fun LocalDate.between(a: MonthDay, b: MonthDay): Boolean = MonthDay.from(this).let{ md ->
    if ( a < b ) {
        a <= md && md <= b        
    } else {
        a <= md || b >= md
    }
}
🤷‍♂️😅
j
this is #getting-started - didn't want too many syntax things, but yeah ( md -> it }
l
I didn't find
MonthDay
in kotlinx.datetime, so probably will revert to Java, because at least here's a reusable, concise function.
j
tbh i haven't found kotlinx.datetime to be a suitable replacement for java.time as of yet, ymmv.
l
Yes, looks still quite in beta
BTW IntelliJ just suggested replacing
a <= md && md <= b
with a range check
md in a..b
.
j
as long as you have some tests then you know it will continue to work.
l
In the end it's just @Javier’s answer, but for
MonthDay
and with safeguards.
Thank you everyone for your kind help!
BTW relevant info
Since a MonthDay does not possess a year, the leap day of February 29th is considered valid
p
I was going to exploit
dayOfYear
, but that would only work if start month < end month. 😅
👍 1
j
by the way, the two functions give quite different results you know. the a..b one is quite different. that's why i would always suggest having some unit tests for this stuff, so that things that look as if you can change them around do actually have the same behaviour.
Copy code
import java.time.LocalDate
import java.time.MonthDay

fun LocalDate.between(a: MonthDay, b: MonthDay): Boolean =
    MonthDay.from(this).let {
        if (a < b) {
            a <= it && it <= b
        } else {
            a <= it || b >= it
        }
    }


//fun LocalDate.between(a: MonthDay, b: MonthDay): Boolean = MonthDay.from(this).let {
//    if (a <= b) {
//        it in a..b
//    } else {
//        it in b..a
//    }
//}


println("Expect true: " + LocalDate.of(2025, 11, 14).between(MonthDay.of(11, 6), MonthDay.of(1, 9)))
println("Expect true: " +LocalDate.of(2025, 12, 14).between(MonthDay.of(11, 6), MonthDay.of(1, 9)))
println("Expect true: " +LocalDate.of(2025, 1, 4).between(MonthDay.of(11, 6), MonthDay.of(1, 9)))

println("Expect false: " +LocalDate.of(2025, 1, 10).between(MonthDay.of(11, 6), MonthDay.of(1, 9)))
println("Expect false: " +LocalDate.of(2025, 11, 5).between(MonthDay.of(11, 6), MonthDay.of(1, 9)))
this isn't a test, clearly, but didn't want to start down a rabbit hole of testing & assertion libraries etc.
l
Oh yes, good point... in my function the
else
case didn't implement wrapping (e.g. between December and January), but just assumed the input order was wrong. Thanks for catching the bug!
blob no problem 1