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

将事件添加到日历时,Android Studio将ISO字符串转换为“ America / New_York”

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

将事件添加到日历时,Android Studio将ISO字符串转换为“ America / New_York”

那是因为您要告诉解析器忽略

Z
,或者说是按字面值匹配,而不是处理它的 意思

输入字符串是

Instant
值,因此解析为
Instant
然后 应用时区。
如果您没有兼容Java
8+的Android,请使用ThreeTenABP。

String stTime = "2018-10-17T22:00:00Z";ZonedDateTime time = Instant.parse(stTime).atZone(ZoneId.of("America/New_York"));System.out.println(time); // prints: 2018-10-17T18:00-04:00[America/New_York]

如果您更喜欢使用过时的

SimpleDateFormat
,只需使用正确的格式,而不指定时区。

SimpleDateFormat formatStart = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX");Date startDate = formatStart.parse(stTime);// My default time zone is America/New_York, so:System.out.println(startDate); // prints: Wed Oct 17 18:00:00 EDT 2018

startDate.getTime()
是您需要的时间。

对于<24级的API,格式模式

X
不起作用,因此您需要
Z
像问题中那样进行硬编码,但是要告诉解析器它采用UTC,因为这就是
Z
时区的含义。

SimpleDateFormat formatStart = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");formatStart.setTimeZone(TimeZone.getTimeZone("UTC"));Date startDate = formatStart.parse(stTime);// My default time zone is America/New_York, so:System.out.println(startDate); // prints: Wed Oct 17 18:00:00 EDT 2018


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

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

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