android - 如何将字符串日期解析为其他时区的其他日期
问题描述
鉴于我有这样的字符串日期
val date = "2019-01-07T13:54:00+0000"
如何将此日期解析为其他时区,例如“亚洲/加尔各答”?
我在尝试:
val zone = DateTimeZone.forID("Asia/Kolkata")
val resultMillis = ISODateTimeFormat
.dateTimeParser()
.withZone(zone)
.parseDateTime(date)
但它没有奏效
解决方案
这是一个两步过程:解析到字符串中的偏移量或时区,然后转换为所需的时区。我只会写 Java 代码,我相信你会翻译:
DateTimeZone zone = DateTimeZone.forID("Asia/Kolkata");
String date = "2019-01-07T13:54:00+0000";
DateTime dt = ISODateTimeFormat.dateTimeParser()
.parseDateTime(date)
.withZone(zone);
System.out.println(dt);
输出是:
2019-01-07T19:24:00.000+05:30
你快到了。我在文本上只是将呼叫交换为withZone
and parseDateTime
。
推荐阅读
- amazon-web-services - 从 API Gateway 调用 Cloudwatch Logs PutLogEvents 操作失败并显示 com.amazon.coral.service#UnknownOperationException
- ruby-on-rails - Heroku 管道阶段是否对应于 Rails 环境阶段?
- acumatica - 如何为添加到网格的用户字段激活过滤操作
- amazon-web-services - 无法将现有 AppSync 解析器/API 连接到现有 Dynamodb 表
- swift - 如何在 UITextView 中使用 NSMutuableAttributedString?
- reactjs - GraphQL(Prisma)突变数据未定义
- npm - 无法构建大型机操作系统开发应用程序
- nlp - 如何训练会导致两个新闻标题之间的相似度得分的模型?
- java - getname() 和 addmemberlist() 的问题
- django - 试图显示最新记录 - Django