DateTimeFormatter class

A formatter and parser for date-time objects based on patterns, similar to Java's DateTimeFormatter.

This class provides pattern-based formatting and parsing of date-time values with support for locales and timezones. It follows Java-like method naming conventions (e.g., getDate(), getPattern()).

Pattern Syntax

The formatter uses the following pattern symbols (case-sensitive):

  • y or yyyy: Year (e.g., 2024)
  • M: Month (1-12)
  • MM: Month (01-12)
  • MMM: Month abbreviation (Jan, Feb, etc.)
  • MMMM: Full month name (January, February, etc.)
  • d: Day of month (1-31)
  • dd: Day of month (01-31)
  • EEE: Day of week abbreviation (Mon, Tue, etc.)
  • EEEE: Full day of week name (Monday, Tuesday, etc.)
  • H: Hour (0-23)
  • HH: Hour (00-23)
  • m: Minute (0-59)
  • mm: Minute (00-59)
  • s: Second (0-59)
  • ss: Second (00-59)
  • S: Millisecond (0-999)
  • SSS: Millisecond (000-999)
  • z: Timezone abbreviation (EST, PST, etc.)
  • zzz: Timezone name (America/New_York, etc.)
  • Z: Timezone offset (+HH:mm)
  • X: ISO 8601 timezone offset (Z for UTC, ±HH:mm for others)

Usage Examples

// Create a formatter with a pattern
final formatter = DateTimeFormatter.ofPattern('dd/MM/yyyy HH:mm:ss', Locale('en', 'US'));

// Format a LocalDateTime
final date = LocalDateTime.of(2024, 6, 27, 14, 30, 45);
final formatted = formatter.getFormattedDate(date);
print(formatted); // Output: 27/06/2024 14:30:45

// Format a ZonedDateTime
final zoned = ZonedDateTime.now(ZoneId.of('America/New_York'));
final withZone = formatter.getFormattedZonedDate(zoned);
print(withZone); // Output with timezone info

// Parse a string back to LocalDateTime
final parsed = formatter.parseDate('27/06/2024 14:30:45');
print(parsed); // LocalDateTime(2024, 6, 27, 14, 30, 45)

Constructors

DateTimeFormatter.ofPattern(String pattern, [Locale? locale, ZoneId? zone])
Creates a DateTimeFormatter with the specified pattern and optional locale and timezone.
factory

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

format(ZonedDateTime zonedDateTime) String
Formats a ZonedDateTime object according to the pattern.
formatDateTime(DateTime dateTime) String
Formats a standard Dart DateTime object according to the pattern.
formatLocalDateTime(LocalDateTime localDateTime) String
Formats a LocalDateTime object according to the pattern.
getFormattedDate(LocalDateTime dateTime) String
Formats a LocalDateTime according to the pattern.
getFormattedDateOnly(LocalDateTime dateTime) String
Formats only the date component of a LocalDateTime.
getFormattedLocalDate(LocalDate date) String
Formats a LocalDate.
getFormattedLocalTime(LocalTime time) String
Formats a LocalTime.
getFormattedTimeOnly(LocalDateTime dateTime) String
Formats only the time component of a LocalDateTime.
getFormattedZonedDate(ZonedDateTime dateTime) String
Formats a ZonedDateTime including timezone information.
getLocale() Locale
Returns the locale.
getPattern() String
Returns the pattern string.
getZone() ZoneId?
Returns the timezone if set, or null.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
parseDate(String dateString) LocalDateTime
Parses a string into a LocalDateTime according to the pattern.
parseLocalDate(String dateString) LocalDate
Parses a string into a LocalDate.
parseLocalTime(String timeString) LocalTime
Parses a string into a LocalTime.
toString() String
A string representation of this object.
override
withZone(Object zone) DateTimeFormatter
Returns a copy of this DateTimeFormatter with the specified time zone.

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Properties

RFC_1123_DATE_TIME DateTimeFormatter
A preconfigured DateTimeFormatter for the RFC-1123 date-time format.
final

Static Methods

getBasicIsoDate() DateTimeFormatter
Date only: "yyyy-MM-dd"
getBasicIsoTime() DateTimeFormatter
Time only: "HH:mm:ss"
getHttpHeaderFormatter() DateTimeFormatter
HTTP header date formatter
getIso8601DateTime() DateTimeFormatter
ISO 8601 formatter: "yyyy-MM-dd'T'HH:mm:ss"
getIso8601DateTimeWithZone() DateTimeFormatter
ISO 8601 with timezone: "yyyy-MM-dd'T'HH:mm:ssZ"
getRfc1123DateTime() DateTimeFormatter
RFC 1123 formatter: "EEE, dd MMM yyyy HH:mm:ss zzz"