java - 我怎样才能在UTC java中获得今天的具体时间

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

我正在尝试获取“今天”的 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); 或者有更好的方法来做到这一点。

最佳答案

应该是这样的

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();

关于java - 我怎样才能在UTC java中获得今天的具体时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48708963/

相关文章:

java - 当我切换断点时,如何使整行在 Eclipse 中改变颜色?

javascript - Moment js判断已经过去了多少天,几周

Java 8 Streams 分组收集器

Java 8 流矩阵乘法比 For 循环慢 10 倍?

java - Lambda 表达式和匿名类之间的实现差异/优化

java - 甲骨文.security.crypto.core.CipherException : Invalid padding string (or incorrect password)

java - 线程中出现异常 "main"java.lang.OutOfMemoryError : unable to create new native thread

java - 编写一个方法,该方法接受一个字符串数组,并按元音数量从小到大对字符串进行排序,并保持字母顺序

c# - 将 YYYYMMDDHHMMSS 的字符串值格式转换为 C# DateTime

c# - 将日期和时间字符串序列化为 DateTime 对象