如何使用java.time.Instant和ChronoUnit获得午夜时间戳的UTC?
我正在尝试获取UTC午夜时间戳的格式。例如,如果今天是2017年2月10日,那么正确的日期:2017年2月份10日星期五00:00:00 UTC,等于时间戳:1486684800000(我想要当前的日期和时间(。
我试过
Instant.now
Instant.now.getEpochSecond to pass in JSON it end up in error
错误:
"statusCode":400,"titleMessage":"Value not allowed","userMessage":"The date with time stamp 2,022 is not in UTC at midnight
请建议的解决方案
截断
您可以去掉日期-时间值中不太重要的部分。
在Java语法中:
Instant
.now()
.truncatedTo( ChronoUnit.DAYS )
.toEpochMilli()