Search code examples
datekotlinandroid-calendardaysandroid-date

How to operate on days in Kotlin?


I can get the current time like System.currentTimeInMillis(). But I really don't need this granularity in the millisecond range. A type which gets me a rough day date like 22.11.2021 is enough for my purposes.

I also would appreciate operations like incrementing the next 14 days etc.


Solution

  • You can use LocalDate as follows:

    fun main() {
        val currentDate = LocalDate.now()
        println(currentDate)
        val futureDate = currentDate.plusDays(14)
        println(futureDate)
    }
    

    You can read more about this new Time API available in Java/Kotlin at https://www.baeldung.com/java-8-date-time-intro.