java - Why does the java.time.Clock has zone information?
问题描述
Why does java.time.Clock
has zone information? From the Clock you only can get an Instant
when calling the instant()
method - which is a time without zone info.
Is the only purpose to have the zone available in the clock to e.g. create a ZonedDateTime like this?
ZonedDateTime.ofInstant(clock().instant(), clock().getZone())
Wouldn't it then make sense to have a method zonedDateTime()
in the Clock class?
解决方案
备用时钟行为
引用Clock
文档(强调我的):
时钟的使用是可选的。所有关键的日期时间类也有一个 now() 工厂方法,它使用默认时区的系统时钟。这种抽象的主要目的是允许在需要时插入备用时钟。应用程序使用对象而不是静态方法来获取当前时间。这可以简化测试。
例如,Clock.fixed( Instant fixedInstant, ZoneId zone )
始终将当前时刻报告为特定时刻,即固定(不变)的时间点。
推荐阅读
- python - 如何聚合字典列表中的某些元素
- go - 如何在 Go 中将文件压缩为没有目录文件夹的 .zip
- excel - VBA代码在可见单元格中粘贴特殊值
- api - Twiitter python API 无法连接流式传输
- git - 尝试通过 SSH 克隆使用了错误的公钥
- ios - Swift - 如何确定 collectionView 的 scrollView.content.y?
- bash - 为什么以下字符串比较会返回不同的结果?
- r - Shiny:渲染输出时如何防止输入重复?
- python - 如何使用 Shopify Python API 包装器对结果进行分页
- python - 如何自定义pytorch数据