I'm working with Dates for the first time in my ap...
# android
c
I'm working with Dates for the first time in my app and I'm using Kotlin. What's the go to for being able to take a date and say "This date is 15 days from now". Should I be looking into java8 desugaring, or does kotlin have some sort of datetime classes that would work for my scenario?
k
I had something similar and I ended up using kotlinx-datetime. It is not 1.0 yet but I found it quite nice.
s
Java desugaring and DateTime works good too.
*LocalDateTime
☝️ 1
c
Okay. I guess I will go the desugaring route as a first try and will try out LocalDateTime.
c
kotlinx-datetime is a wrapper around the Java 8 datetime APIs, so you'd need to use desugaring for that anyway. But yes, enabling desugaring so you can use the
java.time
APIs are the way to go. There's lots of date/time-related classes there which all work very nicely for whatever the scenario you need (LocalDate, LocalDateTime, YearMonth, Month enums, etc.)
c
Awesome. thanks. im a bit timid about enabling desugaring (worried about the compile time hit), but its on my list to get to later today.
c
In my experience, I haven't really noticed any build performance hit with desugaring. Kapt is by far the biggest contributor to compile times that i've noticed. But if you do find it significantly increases compile times (especially incremental build time), you can also use https://github.com/JakeWharton/ThreeTenABP, which is what everyone used to access the java.time APIs before desugaring was a thing. Though you should know that this lib is being deprecated in favor of desugaring. Of course, the age-old
Calendar
and
Date
Java stdlib classes are always an option too, but they both have significant drawbacks, including a pretty horrendous API. You absolutely can compute relative dates/times with them, but it is painful. Definitely prefer the java.time APIs if you can, for your own sanity's sake