Keywords: Dart | DateTime Conversion | FormatException | String Parsing | Flutter Development
Abstract: This article provides an in-depth analysis of FormatException errors encountered during string date format conversion in Dart, detailing the applicable scenarios for DateTime.parse() and DateFormat, offering complete solutions for the 'yyyyMMddHHmmss' format without separators, and demonstrating proper handling of date-time string parsing and formatting through code examples.
Problem Background and Error Analysis
In Dart and Flutter development, handling date-time string conversion is a common requirement. When attempting to convert a string in the format yyyyMMddHHmmss (e.g., 20180626170555) to a DateTime object, developers may encounter the following exception:
FormatException: Trying to read MM from 20180623130424 at position 14This error indicates that the parser encountered an issue while trying to read the month (MM) at position 14. The root cause is that the DateFormat class from the intl package cannot properly handle date-time strings without separators. The entire string is incorrectly parsed as the year part, causing subsequent fields like month and day to fail reading correctly.
Core Solution
The DateTime.parse() method in Dart's core library can handle strings close to the target format. One of the standard formats supported by this method is 20120227T132700, which differs from our target format 20180626170555 mainly by having a T separator between the date and time parts.
Based on this insight, we can adapt to the standard format through simple string processing:
String date = '20180626170555';
String dateWithT = date.substring(0, 8) + 'T' + date.substring(8);
DateTime dateTime = DateTime.parse(dateWithT);This code first splits the original string into the date part (first 8 characters) and the time part (remaining characters), then inserts a T separator between them, and finally uses DateTime.parse() for parsing.
Alternative Approaches Comparison
Although the DateFormat class is powerful in most scenarios, it requires explicit format patterns and separators. For example, to parse a standard format date-time string:
DateTime tempDate = DateFormat("yyyy-MM-dd hh:mm:ss").parse(savedDateString);This approach works well for formats with clear separators but is ineffective for compact formats without separators.
Another common usage is date-time format conversion:
String date = DateFormat("yyyy-MM-dd hh:mm:ss").format(DateTime.now());This demonstrates how to format a DateTime object into a string with a specific pattern.
Practical Recommendations and Considerations
When handling date-time conversions, it is recommended to:
- Prefer using
DateTime.parse()for standard or near-standard formats - Preprocess non-standard format strings to conform to standard formats
- Use the
DateFormatfrom theintlpackage for complex formatting and localization needs - Always handle potential exceptions to ensure application robustness
By understanding the core mechanisms of date-time handling in Dart, developers can more effectively resolve various format conversion issues, improving code quality and maintainability.