datetime - 如何在 Kotlin 中向 LocalDateTime 对象添加时区偏移量?
问题描述
我正在努力将时区为 UTC 的 LocalDateTime 对象转换为韩国标准时间(KST,+09:00)并获取时间戳。我想将2021-04-20T03:00:00
(UTC) 更改为2021-04-20T12:00:00
(KST)。
从数据库中获取了一个 UTC LocalDateTime 对象。
/*
+----+---------------------+
| id | created_at |
+----+---------------------+
| 0 | 2021-04-20T03:00:00 |
+----+---------------------+
*/
val createdAt: LocalDateTime = db.select(created_at).where(id.eq(0)) // 2021-04-20T03:00:00
然后,我尝试使用 OffsetDateTime 将对象的时区更改为 KST(+09:00)。
val kstCreatedAt = OffsetDateTime.of(createdAt, ZoneOffset.of("+9"))
// I expected this is 2021-04-20T12:00:00
我的最终目标是获得2021-04-20T12:00:00
.
kstCreatedAt.toInstant().toEpochMilli() // 1618855200000 (The timestamp of 2021-04-20T03:00:00)
// I expected this is 1618887600000 (The timestamp of 2021-04-20T12:00:00)
我预计变量kstCreatedAt
是2021-04-20T12:00:00
,但它仍然是2021-04-20T03:00:00
,最后一个值是2021-04-20T03:00:00
not的时间戳2021-04-20T12:00:00
。如何将时区偏移量(+9)添加到 LocalDateTime 对象?
解决方案
推荐阅读
- java - java.lang.IllegalStateException:尝试在没有模型的组件上设置模型对象!将 IModel 传递给构造函数或
- javascript - 比较许多 javascript 值
- python - 如何使用标准
- excel - 如何比较同一工作簿但不同工作表中的两个命名范围?
- php - 从前端表单中选择新帖子的类别?
- javascript - 如何将现有函数添加到新的 createElement 元素
- html - 检查是否滚动窗口或通过jquery单击
- ios - Xcode Swift 在“应用程序目标”与“框架目标”中导入代码——有什么区别?
- azure-cosmosdb - Cosmos SDK 3 中的“未定义”#
- python-2.7 - 极坐标图:TypeError:“numpy.float64”对象不能被解释为索引