我怎样才能在UTC中获取特定时间今天

时间:2018-02-09 15:21:54

标签: java datetime java-8 java-time java.time.instant

我想在UTC中获取“今天”的特定时间。 说UTC时间下午5点 Instant.now().truncatedTo(ChronoUnit.DAYS).plus(1, ChronoUnit.DAYS) 要么 Instant.now().plus(1, ChronoUnit.DAYS).truncatedTo(ChronoUnit.DAYS) 我相信这让我到了当天的午夜。我只是扩展它 Instant.now().truncatedTo(ChronoUnit.DAYS).plus(1, ChronoUnit.DAYS).minus(7, ChronoUnit.HOURS); 或者有更好的方法来做到这一点。

1 个答案:

答案 0 :(得分:4)

这将是沿着这些方向发展的事情

ZonedDateTime now = ZonedDateTime.now(ZoneOffset.UTC);
// this would be the today (might be in the past)
ZonedDateTime result = now.with(LocalTime.of(17, 0));
if (result.isBefore(now)) {
  // This would be "next time it is 5 o-clock".
  result = result.plusDays(1);
}
// if you really want an Instant out of it.
return result.toInstant();
相关问题