无法解析的日期(相当琐碎的格式)[重复]

问题描述 投票:-1回答:1
输入:

[Thu,2020年3月12日22:08:53 GMT

代码:

SimpleDateFormat format = new SimpleDateFormat("EEE, dd MM yyyy HH:mm:ss z", Locale.ENGLISH); Date date = null; try { date = format.parse(header); } catch (ParseException e) { e.printStackTrace(); }

例外:

不可分割的日期:“格林尼治标准时间2020年3月12日,星期四”

为什么呢?我已经弄清楚了怎么了。需要社区帮助!

java android simpledateformat
1个回答
1
投票
[MM是带有两位数字的月份,对于Mar这样的月份名称,您需要MMM

SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH);

或者,更好的是,迁移到新的java.time API并按照注释中的建议使用DateTimeFormatter.RFC_1123_DATE_TIME

ZonedDateTime date = ZonedDateTime.parse(header, DateTimeFormatter.RFC_1123_DATE_TIME);

© www.soinside.com 2019 - 2024. All rights reserved.