🌐

Flutter/Dartにて日付のフォーマット時にFormatExceptionが出る場合の対処法

に公開

状況

端折りますが、formatterを用意してパースをすると

main.dart
final _formatter = DateFormat("EEE, d MMM yyyy HH:mm");

_formatter.parse(fooDateTime);

以下の例外が発生

FormatException (FormatException: Trying to read EEE from Wed, 4 October 2023 00:00:00 GMT at position 0)

いくら入力されている値を見直しても間違いはなさそう。。

解決方法

localeを指定してあげるだけ

- final _formatter = DateFormat("EEE, d MMM yyyy HH:mm");
+ final _formatter = DateFormat("EEE, d MMM yyyy HH:mm", "en_US");

おわりに

原因はこれに限らないと思いますが、ひとつのご参考までに🫰🏻
誤りなどがありましたら(優しく)ご指摘ください🫶🏻

Discussion