- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
Java8 Instant.now()
给出已采用 UTC 格式的当前时间。我在 CompileJava.net 上创建了一个简单的沙箱测试验证我的预期结果:
Timestamp createdDateUtc = Timestamp.from(Instant.now());
System.out.println(createdDateUtc);
LocalDateTime databaseUtcLocal = createdDateUtc.toLocalDateTime();
ZonedDateTime databaseUtcZoned = databaseUtcLocal.atZone(ZoneId.of("UTC"));
ZonedDateTime userTimezoneTime = databaseUtcZoned.withZoneSameInstant(ZoneId.of("Asia/Qatar"));
System.out.println(userTimezoneTime.format(DateTimeFormatter.ofPattern("MM/dd/yyyy hh:mm:ss a")));
注意:当前时间为 2018 年 12 月 1 日上午 11:16 UTC。
当我在在线编译器上运行此代码时,我得到输出:
2018-12-01 11:17:43.637
12/01/2018 02:17:43 PM
但是当我在本地 JBOSS 服务器上运行此代码时,我得到以下输出:
2018-12-01 03:18:07.464
12/01/2018 06:18:07 AM
这里可能出了什么问题?我的服务器 UTC 时钟是否不正确?我的本地服务器的物理位置位于加利福尼亚州。虽然这并不重要,但当我尝试使用 Instant.now()
显示 UTC 时间时,我却得到了太平洋时间,这似乎很奇怪。
非常感谢任何帮助!
更新
正如已接受的答案所指出的,问题是我的 Timestamp.from()
方法:
System.out.println(Instant.now());
System.out.println(Timestamp.from(Instant.now()));
输出:
2018-12-01T11:48:33.153Z
2018-12-01 03:48:33.171
所以现在我的问题发生了变化。如果您有类似的问题,我在 Stack Overflow here 上找到了我的答案。我使用了这个丑陋的困惑,而不是 Timestamp.from(Instant.now())
:
Timestamp.valueOf(ZonedDateTime.of(LocalDateTime.now(), ZoneId.systemDefault()).withZoneSameInstant(ZoneId.of("UTC")).toLocalDateTime())
最佳答案
我认为 Timestamp.toString()
方法有问题。如果你仔细观察它的内部,你会看到:
public String toString () {
int year = super.getYear() + 1900;
int month = super.getMonth() + 1;
int day = super.getDate();
int hour = super.getHours();
int minute = super.getMinutes();
int second = super.getSeconds();
// other code
}
方法 getHours()
、getMinutes()
和其他方法来自 Date
类,并根据文档按本地时区 Date.getHours() 进行解释。
关于java - 为什么 Java 8 Instant.now() 在我的本地服务器上显示错误的 UTC?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53570315/
例如,如果一个 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 的输入,我想比较它们。
我是一名优秀的程序员,十分优秀!