How to calculate the date six months from the current date in Kotlin

1 Answer

0 votes
import java.time.LocalDate
import java.time.format.DateTimeFormatter

fun addMonthsToDate(months: Long, date: LocalDate = LocalDate.now()): LocalDate {
    return date.plusMonths(months)
}

fun main() {
    val futureDate = addMonthsToDate(6)
    val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd")

    println("Date six months from now: ${futureDate.format(formatter)}")
}

 
  
/*
run:
  
Date six months from now: 2025-12-12

*/

 



answered Jun 12, 2025 by avibootz
...