Home > Mobile >  Kotlin -> Timestamp of today's date at a specific time
Kotlin -> Timestamp of today's date at a specific time

Time:11-18

I am trying to get the timestamp of today's date at 4am. So when the API is called, on that day --4am. I use offsetDateTime.now(), to get the current time, but is there a way to add a parameter to give me the timestamp of today's date at a specific hour >

  val now = OffsetDateTime.now()

CodePudding user response:

You should be able to use OffsetDateTime's withX functions to override specific fields in the date time:

val todayAt4am = OffsetDateTime.now()
    .withHour(4)
    .withMinute(0)
    .withSecond(0)
    .withNano(0)

Or more concisely using truncatedTo:

val todayAt4am = OffsetDateTime.now()
    .truncatedTo(ChronoUnit.HOURS) // sets smaller units to 0
    .withHour(4)
  • Related