问题描述
我想弄清楚如何将 LocalDateTime 转换为偏移量为 0 的 Date 对象。
我正在使用的将 LocalDateTime 转换为 Date 的当前代码是:
Date.from(localDateTime.toInstant(ZoneOffset.UTC))
本地日期时间:
2016-12-07T16:29:12.218
转换为Date后:
2016-12-07T10:29:12.218-0600
是的,我看到有一个区域偏移量被传递到 toInstant,但我不确定如何做我所要求的。
更新: 我刚刚收到有关我的问题的更多信息,说要设置精确的日期和 UTC 区域以使偏移量为 0。
解决方法
您似乎正在尝试将系统区域中的 LocalDateTime 转换为 Date。所以,试试这个
Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant())