将 DateFormat 与区域设置一起使用会导致 FormatException: Trying to read yyyy from 2020-08-07 15:00 atposition 0

Ahm*_*gdi 7 datetime android date ios dart

DateFormat当我使用以下语言环境时,\'ar\'我可以在 flutter 中使用RTL语言环境。这是我的代码:

\n
String finalDatetime = \'$dateString $timeString\';\nDateTime tempDate = new DateFormat("yyyy-MM-dd hh:mm").parse(finalDatetime);\nString formattedDate = Intl.getCurrentLocale() == \'en\' ? DateFormat(\'dd-MM-yyyy \xe2\x80\x93 hh:mm\').format(tempDate)\n:DateFormat.yMMMd(\'ar\').format(tempDate);\n
Run Code Online (Sandbox Code Playgroud)\n

我在这一行收到错误:

\n
DateTime tempDate = new DateFormat("yyyy-MM-dd hh:mm").parse(finalDatetime);\n
Run Code Online (Sandbox Code Playgroud)\n

错误

\n
FormatException: Trying to read yyyy from 2020-08-07 15:00 at position 0\n
Run Code Online (Sandbox Code Playgroud)\n

笔记

\n
    \n
  • 当语言环境设置为\'en\'
  • \n
  • 日期字符串是从数据库中获取的,该数据库始终作为2020-08-07 15:00
  • \n
\n

Ahm*_*gdi 6

正如Jamesdlin他的评论中提到的,使用Intl.withLocale解决了问题

DateTime tempDate =Intl.withLocale('en', () => new DateFormat("yyyy-MM-dd hh:mm").parse(finalDatetime));
Run Code Online (Sandbox Code Playgroud)