Kotlin: Add 7 days to today's date
Video: Kotlin: Add 7 days to today's date by Taught by Celeste AI - AI Coding Coach
Watch full page →Kotlin: Add 7 Days to Today's Date
In Kotlin, working with dates is straightforward using the java.time API. This example shows how to get the current date and add 7 days to it, which is useful for date calculations like setting deadlines or scheduling events.
Code
import java.time.LocalDate
fun main() {
// Get today's date
val today = LocalDate.now()
println("Today: $today")
// Add 7 days to today's date
val futureDate = today.plusDays(7)
println("Date after 7 days: $futureDate")
}
Key Points
- Use LocalDate.now() to get the current date without time information.
- The plusDays() method adds a specified number of days to a LocalDate instance.
- java.time API provides immutable date objects, so methods return new instances.
- This approach avoids deprecated Date and Calendar classes for clearer date manipulation.