LocalDateTime.parse constructor
LocalDateTime.parse(
- String dateTimeString
Parses a string in ISO 8601 format: YYYY-MM-DDTHH:mm[:ss[.SSS]]
Throws InvalidFormatException for invalid formats.
Example:
final parsed = LocalDateTime.parse("2025-06-27T08:15:30.123");
A date-time object without a timezone, composed of a LocalDate and LocalTime.
This class represents a specific moment on a calendar, combining a date and time without referencing any timezone. It is immutable and supports arithmetic and comparison operations.
Example:
final dateTime = LocalDateTime.of(2024, 12, 31, 23, 59);
print(dateTime); // 2024-12-31T23:59:00
Implementation
factory LocalDateTime.parse(String dateTimeString) {
final parts = dateTimeString.split('T');
if (parts.length != 2) {
throw InvalidFormatException('Invalid datetime format. Expected YYYY-MM-DDTHH:mm:ss');
}
final date = LocalDate.parse(parts[0]);
final time = LocalTime.parse(parts[1]);
return LocalDateTime(date, time);
}