kotlin + localdatatime yesterday
To work with Kotlin and LocalDateTime
to get yesterday's date, you can follow these steps:
Import the necessary classes:
kotlin import java.time.LocalDateTime import java.time.temporal.ChronoUnit
Get the current date and time:
kotlin val currentDateTime = LocalDateTime.now()
Subtract one day from the current date and time using the
minus
method:kotlin val yesterdayDateTime = currentDateTime.minus(1, ChronoUnit.DAYS)
Access the desired components of the
LocalDateTime
object:To get the year:
kotlin val year = yesterdayDateTime.year
To get the month:
kotlin val month = yesterdayDateTime.month
To get the day of the month:
kotlin val dayOfMonth = yesterdayDateTime.dayOfMonth
To get the hour:
kotlin val hour = yesterdayDateTime.hour
To get the minute:
kotlin val minute = yesterdayDateTime.minute
To get the second:
kotlin val second = yesterdayDateTime.second
To get the nanosecond:
kotlin val nano = yesterdayDateTime.nano
Note: Make sure to handle any exceptions that may occur when working with LocalDateTime
.