Dart:解析日期时区给出 UnimplementedError
Dart : parse date timezone gives UnimplementedError
我需要在我的 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: Characters remaining after date parsing in
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
实施了?
遗憾的是 z
和 v
模式没有实现。
在 Dart DateTime 具有时区信息之前,这些不会被实施
DateFormat is for formatting and parsing dates in a locale-sensitive manner.
您要求解析的不是 locale-sensitive 日期字符串,而是 ISO 8601 日期字符串,因此您不应使用 DateFormat
class。
而是使用 DateTime.parse
method,它根据其文档支持您描述的格式:
Examples of accepted strings:
- ...
- "2002-02-27T14:00:00-0500": Same as "2002-02-27T19:00:00Z"
我需要在我的 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: Characters remaining after date parsing in 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
实施了?
遗憾的是 z
和 v
模式没有实现。
在 Dart DateTime 具有时区信息之前,这些不会被实施
DateFormat is for formatting and parsing dates in a locale-sensitive manner.
您要求解析的不是 locale-sensitive 日期字符串,而是 ISO 8601 日期字符串,因此您不应使用 DateFormat
class。
而是使用 DateTime.parse
method,它根据其文档支持您描述的格式:
Examples of accepted strings:
- ...
- "2002-02-27T14:00:00-0500": Same as "2002-02-27T19:00:00Z"