Search code examples
jsonscaladatetimetimestamp

How to obtain "UTC at Midnight" Timestamp with java.time.Instant and ChronoUnit


How to obtain UTC at Midnight Timestamp with java.time.Instant and ChronoUnit?

I am trying to get UTC at Midnight Timestamp in format. For example, if today were Feb 10 2017, then correct date: Fri Feb 10 00:00:00 UTC 2017 which is equal to timestamp: 1486684800000 (I want for current date and time).

I tried

Instant.now
Instant.now.getEpochSecond to pass in JSON it end up in error

Error :

"statusCode":400,"titleMessage":"Value not allowed","userMessage":"The date with time stamp 2,022 is not in UTC at midnight

Please suggest a solution


Solution

  • Truncate

    You can lop off the less significant parts of a date-time value.

    In Java syntax:

    Instant
    .now()
    .truncatedTo( ChronoUnit.DAYS )
    .toEpochMilli()