Search code examples
postgresqlkotlinjooqepochdatetimeoffset

How to convert an Epoch to a OffsetDateTime in Kotlin?


The postgresql Timestamp with Time Zone data type needs to be supplied an OffsetDateTime when being called using a High level language like Kotlin. I could not find a direct method that supports Epoch to OffsetDateTime conversion.


Solution

  • fun convertEpochToOffsetDateTime(epochValue: Long): OffsetDateTime {
      return OffsetDateTime.of(LocalDateTime.ofEpochSecond(epochValue, 0, ZoneOffset.UTC), ZoneOffset.UTC)
    } 
    

    Btw, I'm using Jooq for SQL queries using Kotlin. The above works like a charm.