Dart:解析日期时区给出UnimplementedError

时间:2019-05-17 15:22:24

标签: date dart flutter timezone date-formatting

我需要在我的 Flutter 应用程序(来自JSON)中以以下格式解析日期

2019-05-17T15:03:22.472 + 0000

根据the documentation,我必须使用Z来获取时区(RFC 822格式的最后5个字符),所以我使用以下内容:

new DateFormat("y-M-d'T'H:m:s.SZ").parseStrict(json['startDate']);

但是失败并显示错误:

  

FormatException:日期解析后剩余的字符   2019-05-17T15:03:22.472 + 0000

这是另一个测试:

/// THIS WORKS
try {
    print(new DateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS").format(DateTime.now()));
} catch (e) {
    print(e.toString());
}

/// THIS RETURNS `UnimplementedError` (as soon as I use a 'Z' or 'z') somewhere
try {
    print(new DateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").format(DateTime.now()));
} catch (e) {
    print(e.toString());
}

Z是否已实施?

2 个答案:

答案 0 :(得分:2)

无法实现zv模式。

在Dart DateTime具有时区信息之前,不会实现这些功能

有关此问题的更多信息https://github.com/dart-lang/intl/issues/19

答案 1 :(得分:1)

来自DateFormat class docs

  

DateFormat用于以对语言环境敏感的方式格式化和解析日期。

您不是要解析对语言环境敏感的日期字符串,而是要解析ISO 8601日期字符串,因此,您不应使用DateFormat类。

请改为使用DateTime.parse method,该文档根据您的文档支持您描述的格式:

  

可接受的字符串示例:

     
      
  • ...
  •   
  • “ 2002-02-27T14:00:00-0500”:与“ 2002-02-27T19:00:00Z”相同
  •