- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我尝试将 ZonedDateTime
转换为 Timestamp
时一切正常,直到我在以下代码中调用 Timestamp.from()
:
ZonedDateTime currentTimeUTC = ZonedDateTime.now(ZoneOffset.UTC);
currentTimeUTC = currentTimeUTC.minusSeconds(currentTimeUTC.getSecond());
currentTimeUTC = currentTimeUTC.minusNanos(currentTimeUTC.getNano());
return Timestamp.from(currentTimeUTC.toInstant());
ZonedDateTime.now(ZoneOffset.UTC); -> 2018-04-26T12:31Z
currentTimeUTC.toInstant() -> 2018-04-26T12:31:00Z
Timestamp.from(currentTimeUTC.toInstant()) -> 2018-04-26 14:31:00.0
// (with Timezone of Europe/Berlin, which is currently +2)
为什么 Timestamp.from()
不注意即时设置的时区?
最佳答案
Instant
类没有时区,它只有自 unix 纪元以来的秒和纳秒值。 Timestamp
也表示(从纪元开始的计数)。
why is the debugger displaying this with a Z behind it?
问题出在 toString
方法中:
Instant.toString()
将秒和纳秒值转换为相应的 UTC 日期/时间 - 因此最后是“Z” - 我相信这样做是为了方便(使 API 更加“开发人员友好”)。
A string representation of this instant using ISO-8601 representation.
The format used is the same asDateTimeFormatter.ISO_INSTANT
.
如果我们看一下 DateTimeFormatter.ISO_INSTANT
javadoc :
The ISO instant formatter that formats or parses an instant in UTC, such as '2011-12-03T10:15:30Z'
由于调试器通常使用 toString
方法来显示变量值,这解释了为什么您看到 Instant
最后带有“Z”,而不是秒/纳秒值(value)观。
另一方面,Timestamp.toString
使用 JVM 默认时区将秒/纳秒值转换为日期/时间字符串。
但是 Instant
和 Timestamp
的值是一样的。您可以通过调用方法 Instant.toEpochMilli
和 Timestamp.getTime
来检查两者是否会返回相同的值。
注意:您可以使用 truncatedTo
方法代替调用 minusSeconds
和 minusNanos
:
ZonedDateTime currentTimeUTC = ZonedDateTime.now(ZoneOffset.UTC);
currentTimeUTC = currentTimeUTC.truncatedTo(ChronoUnit.MINUTES);
这会将所有小于 ChronoUnit.MINUTES
的字段(在本例中为秒和纳秒)设置为零。
你也可以使用 withSecond(0)
和 withNano(0)
,但在这种情况下,我认为 truncatedTo
更好,更多开门见山。
注意 2:java.time API 的创建者还为 Java 6 和 7 做了一个反向移植,在项目的 github 问题中 you can see a comment关于 Instant.toString
的行为。这个问题的相关部分:
If we were really hard line, the toString of an Instant would simply be the number of seconds from 1970-01-01Z. We chose not to do that, and output a more friendly toString to aid developers
这强化了我的观点,即 toString
方法的设计是为了方便和易于使用。
关于java - Timestamp.from 不注意 Instant 的时区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50043107/
例如,如果一个 Instant 在其他两个 Instant 的开始和结束范围内,您想在 Kotlin 中检查,您可以简单地执行以下操作: import java.time.Instant val s
Google 即搜即得是一种新的搜索增强功能,可在您键入时显示结果。在 the information page for Google Instant ,它说“15 项新技术有助于 Google 即搜
我定义了以下模型 public class ItemDetail { private final String name; private final String id; p
游戏已启动,我收到了用户的状态,一切就绪。我正在尝试构建一个问答游戏。我正在从远程服务器获取所有信息,其中包括基于问题的图像 Assets 。我可以获取远程数据,但无法显示图像。看来 facebook
我正在尝试生成 java.time.Instant 的实例用于基于属性的测试,并使用Temporal.range几乎适用于所有 Temporal我尝试过的子类- 除了Instant ,这似乎没有提供有
时间戳将存储在数据库中。确定一个名为“b”的时间戳是否是另一个名为“a”的时间戳的后一天的解决方案是什么? 时区将作为参数提供。 例如,考虑: Instant a = Instant.ofEpochS
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 8 年前。 Improve
我想权衡我购买其中一个或两个订阅的决定。我想得到一些数据来支持我的决定。是否可以编写一个程序来比较库?可以从 netflix 和 amazon 获得 API 吗? 最佳答案 除了 scaba 的回答之
我有一个 Instant (org.joda.time.Instant) 的实例,我在一些 api 响应中得到它。我有另一个来自 (java.time.Instant) 的实例,这是我从其他调用中获得
我正在尝试将 org.thirden.bp.LocalDate 转换为 java.util.Date,并且收到问题标题中提到的错误。 我使用以下进行转换: Date.from(currentDate.
对于我的每个用户,我存储一个 tzid我将其转换为 DateTimeZone持有有关其本地时区的信息。 我想在本地时间每天早上 8 点向用户发送一封电子邮件;如果上午 8 点由于某种原因(例如夏令时类
本文将提供如何在 Java LocalDateTime 和 Instant 之间进行转换。 LocalDateTime 表示没有时区的日期时间,例如 2019-10-25T12:15:30,而 Ins
这个问题在这里已经有了答案: 10年前关闭。 Possible Duplicate: How does Google Instant work? 我们经常使用谷歌搜索任何内容,但是当我们在谷歌引擎的文
我有一个设计类似于 myspace/facebook 类型社区的社交网站,我在 LAMP 设置上使用 php/mysql。 我一直想拥有自己的即时通讯工具,可以在用户的 PC 上运行,类似于 AI
我正在为我的网站开发私有(private)消息传递功能, 有没有办法从服务器获取用户收到新消息的推送通知? 我知道的唯一技术是通过 ajax 不断轮询服务器以查看是否有新消息,并在需要时重新加载信使窗
我越来越了解NodaTime并且非常喜欢它。但是我还不太了解! 给定诸如“ 2014-04-08T09:30:18Z”的值,将这样的字符串解析为NodaTime Instant所需的步骤是什么? 谢谢
这个问题已经有答案了: How to unify date format using DateTimeFormatter (4 个回答) What is the Standard way to Par
我正在尝试根据 B.C.E. 创建一个 Instant。公历年。 这是我目前所拥有的: Instant.FromDateTimeOffset(new DateTimeOffset(-1000, 10,
我已尝试获取特定时区的当前实例,但它没有按预期工作。关于如何执行此操作的任何想法? 这是我做的: Instant.now(Clock.system(ZoneId.of("America/Los_Ang
这个问题可能与另一个问题类似: HTML Comparing 2 Dates with Javascript 但我不想使用按钮,只需让它即时即可。 我有 2 个类型为 date 的输入,我想比较它们。
我是一名优秀的程序员,十分优秀!