无法在 dart 中使用 intl 的 DateFormat 解析 yyyyMMdd

Sir*_*lan 2 datetime date-formatting dart

我有一个像“20200814”这样的日期字符串,表示 2020 年 08 月 14 日。

\n

在 intl 的文档中是这样说的:

\n

'yyyy.MM.dd G 'at' HH:mm:ss vvvv' 1996.07.10 AD at 15:08:56 Pacific Time

\n

所以如果我用这个:

\n

DateFormat('yyyyMMdd').parse('20200814')

\n

它必须有效,但会抛出此错误:

\n
\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90 Exception caught by animation library \xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\nThe following FormatException was thrown while notifying status listeners for AnimationController:\nTrying to read MM from 20200814 at position 8\n\nWhen the exception was thrown, this was the stack\n#0      _DateFormatField.throwFormatException \npackage:intl/\xe2\x80\xa6/intl/date_format_field.dart:87\n#1      _DateFormatPatternField.parseField \npackage:intl/\xe2\x80\xa6/intl/date_format_field.dart:337\n#2      _DateFormatPatternField.parse \npackage:intl/\xe2\x80\xa6/intl/date_format_field.dart:250```\n
Run Code Online (Sandbox Code Playgroud)\n

Mic*_*orn 6

我查看了 intl 源代码,不幸的是该库不支持解析包含不由非数字字符分隔的数字组件的日期字符串。

这样做的原因是,parse(input)将输入字符串转换为流,当它尝试提取年份值时,它会调用输入流上的_Stream实例方法_Stream.nextInteger(),然后该方法会消耗整个字符串,因为整个字符串可以读取为单个整数。这使得流中没有任何内容可以被解析为月份或日期,这就是抛出错误的原因。