我正在 EST 时区运行我的代码。
在我的代码中使用 Instant.now()
并返回 UTC 时间。
但是,我正在尝试测试一种方法,该方法从数据库获取数据作为日期而不是即时数据,因此尝试使用
将其转换为日期Date.from(Instant.now())
由于我在美国东部时间运行此程序,因此这个日期
为我提供了美国东部时间的时间。
实际代码,
final Optional<Date> dbTime = dbService.getUpdatedTime();
final Instant lastInstant = dbTime.orElseGet(() -> Date.from(Instant.now())).toInstant();
测试代码,
final Date dbTime = Date.from(Instant.now().minusSeconds(36000));
when(dbService.getUpdatedTime().thenReturn(Optional.of(dbTime));
这里,dbTime
被转换为 EST 时间。我可以通过设置 TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
还有其他更好的办法吗?在主应用程序类中设置 TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
以便始终将其视为 UTC 可以吗?
最佳答案
第一个建议,既然你可以使用现代的Java日期和时间API,那就尽可能多地使用它,并尽量减少使用过时的 Date
类(class)。如果您可以修改 getUpdatedTime()
那就最好了返回Optional<Instant>
而不是Optional<Date>
(现代 JDBC 驱动程序可以直接为您提供数据库中的日期时间 Instant
)。自 Instant
以 UTC 格式打印,这应该会消除您所有的问题和疑问。
在这个答案中,我假设您要么不能这样做,要么暂时不想这样做。不过,您仍然可以接近:
final Optional<Instant> dbTime = dbService.getUpdatedTime().map(Date::toInstant);
final Instant lastReconInstant = dbTime.orElseGet(Instant::now);
避免TimeZone.setDefault()
。由于 JVM 只有一个全局时区设置,这可能会无意中改变程序其他部分或同一 JVM 中运行的其他程序的行为。
一个细节,我建议在您的 stub 代码中明确说明减去 10 小时。两个选项是
final Date dbTime = Date.from(Instant.now().minus(10, ChronoUnit.HOURS));
final Date dbTime = Date.from(Instant.now().minus(Duration.ofHours(10)));
尽管如此,我仍然觉得你一开始就没有问题。一个Date
其中没有时区。它的toString
方法只是获取 JVM 的默认时区并使用它来呈现日期和时间。这愚弄了很多人,这只是尽可能避免上这门课的原因之一。
关于Java 即时到日期 - 具有不同的时区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47279299/