栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

无法从TemporalAccessor获取OffsetDateTime

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

无法从TemporalAccessor获取OffsetDateTime

问题在于,a

ZoneIdis
和a
ZoneOffsetis
之间存在差异。要创建一个
OffsetDateTime
,您需要一个区域偏移量。但是,a
ZoneId
和a之间没有一对一的映射关系,
ZoneOffset
因为它实际上取决于当前的夏时制时间。对于ZoneId像“欧洲/柏林”一样的商品,夏季有一个偏移量,而冬季有一个不同的偏移量。

在这种情况下,使用

aZonedDateTime
代替会更容易
OffsetDateTime
。在解析期间,
ZonedDateTime
将正确设置为
"Europe/Berlin"
区域ID,并且还会根据要解析的日期有效的夏时制设置偏移量:

public static void main(String[] args) {    String datum = "20130419233512";    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss").withZone(ZoneId.of("Europe/Berlin"));    ZonedDateTime datetime = ZonedDateTime.parse(datum, formatter);    System.out.println(datetime.getZone()); // prints "Europe/Berlin"    System.out.println(datetime.getOffset()); // prints "+02:00" (for this time of year)}

请注意,如果您确实要使用OffsetDateTime,可以使用

ZonedDateTime.toOffsetDateTime()
将转换
ZonedDateTime
OffsetDateTime



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/463738.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号