I have a Java Date (Fri Jun 28 10:00:01 GMT+01:00 2024
) which I want to convert to Epoch millis to send to the backend.
I have tried the following:
typealias Dates = java.util.Date
fun Dates.toEpochMillis(timeZoneId: ZoneId): Long {
val one = Instant.ofEpochMilli(time).toEpochMilli() // 1719565201000
val three = secsToMillis(this.toLocalDateTime(timeZoneId).utcEpochSecs) // 1719568801000
return one
}
fun Dates.toLocalDateTime(timeZoneId: ZoneId): LocalDateTime {
return Instant.ofEpochMilli(time).atZone(timeZoneId).toLocalDateTime()
}
fun secsToMillis(timeInSecs: Long): Long {
return timeInSecs * 1000
}
Which of the above is correct or do you suggest any other way of doing this?
You can use this:
fun Dates.toEpochMillis(): Long =
toInstant().toEpochMilli()