java - 转换为日期时间格式“2020-02-11T17:26:31-05:00”
问题描述
嗨,我无法理解我们需要使用什么时间格式来解析这个日期2020-02-11T17:26:31-05:00
我尝试使用日期格式化程序和简单的日期格式,但它不起作用
日期以这种形式出现 -> 2020-02-11T17:26:31-05:00我无法识别此日期的类型
下面是我尝试过的代码片段,但它抛出异常
DateTimeFormatter responseFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss-SSSXXX'Z'",
Locale.ENGLISH);
responseDateTime = LocalDateTime.parse(otmmResponseDate, responseFormatter);
解决方案
请注意,您的日期字符串中有一个偏移量-05:00
。因此,您的字符串不代表 a LocalDateTime
,而是 a OffsetDateTime
,并且应该被解析OffsetDateTime.parse
(并非所有内容都是 a LocalDateTime
!):
// the format is ISO 8601, so it can be parsed directly without a DateTimeFormatter
OffsetDateTime odt = OffsetDateTime.parse("2020-02-11T17:26:31-05:00");
如果您只想要其中的本地日期时间部分,那么您可以toLocalDateTime
稍后调用:
LocalDateTime ldt = odt.toLocalDateTime();
推荐阅读
- javascript - Node/Puppeteer:尝试使用选择器获取所有链接,获取结果属性
- python-3.x - PyInstaller exe 仅适用于 -F 选项
- r - randomForest.default(x, y, mtry = param$mtry, ...) 中的错误:需要至少两个类来进行分类
- ios - 刷新 TabView 选项卡中的数据
- amazon-web-services - 将 Oracle DB(本地)中的 18TB 数据迁移到 AWS 的最佳方法是什么?
- c# - Microsoft Graph - 限制日历访问后无法访问用户配置文件数据
- mysql - 添加新列并替换 NULL 值
- smartsheet-api - Smartsheet API 用户级别
- c# - 正则表达式以正确的顺序替换日期
- java - JavaFX:如何在调整一个/两个多边形的大小(缩放)时保持两个多边形之间的距离不变?