kotlin + localdatatime yesterday

To work with Kotlin and LocalDateTime to get yesterday's date, you can follow these steps:

  1. Import the necessary classes: kotlin import java.time.LocalDateTime import java.time.temporal.ChronoUnit

  2. Get the current date and time: kotlin val currentDateTime = LocalDateTime.now()

  3. Subtract one day from the current date and time using the minus method: kotlin val yesterdayDateTime = currentDateTime.minus(1, ChronoUnit.DAYS)

  4. Access the desired components of the LocalDateTime object:

  5. To get the year: kotlin val year = yesterdayDateTime.year

  6. To get the month: kotlin val month = yesterdayDateTime.month

  7. To get the day of the month: kotlin val dayOfMonth = yesterdayDateTime.dayOfMonth

  8. To get the hour: kotlin val hour = yesterdayDateTime.hour

  9. To get the minute: kotlin val minute = yesterdayDateTime.minute

  10. To get the second: kotlin val second = yesterdayDateTime.second

  11. To get the nanosecond: kotlin val nano = yesterdayDateTime.nano

Note: Make sure to handle any exceptions that may occur when working with LocalDateTime.