【问题标题】:Unparseable date: "2018-07-03T01:00:21.000+0000" Cannot parse this format [duplicate]无法解析的日期:“2018-07-03T01:00:21.000+0000”无法解析这种格式[重复]
【发布时间】:2018-07-10 12:25:08
【问题描述】:

尝试1:

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS+/-HHmm");
format.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date = format.parse(createdDate2);

尝试2:

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd\'T\'HH:mm:ss.SSS\'Z\'");
format.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date = format.parse(createdDate2);

尝试 3:

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
format.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date = format.parse(createdDate2);

这种格式似乎不起作用:

有什么帮助吗?

【问题讨论】:

  • 您使用SimpleDateFormatTimeZoneDate 类的任何特殊原因?这些都已经过时了,SimpleDateFormat 特别是出了名的麻烦。今天我们在java.time, the modern Java date and time API 中做得更好。
  • 猜猜看,为什么 Oracle 没有完全弃用 Date 对象?因为仍然有很多代码使用日期对象...

标签: java simpledateformat


【解决方案1】:
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
        format.setTimeZone(TimeZone.getTimeZone("GMT"));
        Date date = format.parse(createdDate2);

【讨论】:

    【解决方案2】:

    这可能不是您想要的,但如果时区偏移量将使用冒号分隔符写入,例如+00:00ISO_OFFSET_DATE_TIME

    OffsetDateTime d = OffsetDateTime.parse("2018-07-03T01:00:21.000+00:00", DateTimeFormatter.ISO_OFFSET_DATE_TIME);
    System.out.println(d); // 2018-07-03T01:00:21Z
    

    【讨论】:

    • 好建议。要将+0000 解析为偏移量,您可以使用格式模式字符串构建DatetimeFormatter,并将xx 用于偏移量。
    • 当我尝试将此字符串解析为 CharacterSequence 时:'2018-07-10T01:00:03.000+0000' 我收到此错误:java.time.format.DateTimeParseException: Text '2018-07-10T01:00:03.000+0000' could not be parsed at index 23
    • @NagendraSingh 那是因为时区偏移中没有冒号。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-17
    • 1970-01-01
    • 1970-01-01
    • 2017-12-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多