ZonedDateTime class
Represents a date-time with timezone information.
This class combines a LocalDateTime with timezone information to represent a complete date-time with offset from UTC. Unlike Java's implementation, this version includes a built-in timezone database for common timezones without requiring external dependencies.
Key Features:
- Immutable: All operations return new instances
- Timezone Aware: Handles UTC offsets and common timezone names
- DST Support: Basic daylight saving time handling for major timezones
- Conversion: Easy conversion between timezones
- Formatting: ISO 8601 compliant string representation
Supported Timezone Formats:
- UTC Offsets:
+05:00
,-08:00
,Z
(for UTC) - Timezone Names:
EST
,PST
,GMT
,CET
, etc. - Full Names:
America/New_York
,Europe/London
, etc.
Usage Examples:
// Create from current time
final now = ZonedDateTime.now();
final nowInParis = ZonedDateTime.now(ZoneId.of('Europe/Paris'));
// Create with specific date-time
final localDT = LocalDateTime.of(2023, 12, 25, 15, 30);
final christmas = ZonedDateTime.of(localDT, ZoneId.of('America/New_York'));
// Parse from string
final parsed = ZonedDateTime.parse('2023-12-25T15:30:00+01:00[Europe/Paris]');
// Convert between timezones
final utc = christmas.toUtc();
final tokyo = christmas.withZoneSameInstant(ZoneId.of('Asia/Tokyo'));
// Arithmetic operations
final tomorrow = christmas.plusDays(1);
final nextHour = christmas.plusHours(1);
Timezone Database:
This implementation includes a comprehensive timezone database with:
- Major world timezones with their UTC offsets
- Daylight saving time rules for common timezones
- Historical timezone data for accurate conversions
- Support for both abbreviated and full timezone names
- Implemented types
Constructors
- ZonedDateTime.fromDateTime(DateTime dateTime, [ZoneId? zone])
-
Creates a ZonedDateTime from a standard DateTime object.
factory
- ZonedDateTime.fromEpochMilli(int epochMilli, [ZoneId? zone])
-
Creates a ZonedDateTime from milliseconds since the Unix epoch.
factory
- ZonedDateTime.now([ZoneId? zone])
-
Creates a ZonedDateTime from the current date and time in the specified timezone.
factory
- ZonedDateTime.of(LocalDateTime localDateTime, ZoneId zone)
-
Creates a ZonedDateTime with the specified LocalDateTime and ZoneId.
factory
- ZonedDateTime.parse(String dateTimeString)
-
Parses a ZonedDateTime from its string representation.
factory
Properties
- day → int
-
Gets the day of the month component (1-31).
no setter
- dayOfWeek → int
-
Gets the day of the week (1 = Monday, 7 = Sunday).
no setter
- dayOfYear → int
-
Gets the day of the year (1-366).
no setter
- hashCode → int
-
Returns the hash code for this ZonedDateTime.
no setteroverride
- hour → int
-
Gets the hour component (0-23).
no setter
- isDaylightSaving → bool
-
Indicates whether this date-time is in daylight saving time.
no setter
- localDateTime → LocalDateTime
-
Gets the local date-time component (without timezone information).
no setter
- millisecond → int
-
Gets the millisecond component (0-999).
no setter
- minute → int
-
Gets the minute component (0-59).
no setter
- month → int
-
Gets the month component (1-12).
no setter
- offset → Duration
-
Gets the UTC offset for this date-time.
no setter
- offsetInMilliseconds → int
-
Gets the UTC offset in milliseconds.
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- second → int
-
Gets the second component (0-59).
no setter
- year → int
-
Gets the year component.
no setter
- zone → ZoneId
-
Gets the timezone information.
no setter
Methods
-
compareTo(
ZonedDateTime other) → int -
Compares this ZonedDateTime with another for ordering.
override
-
isAfter(
ZonedDateTime other) → bool - Checks if this date-time is after another.
-
isBefore(
ZonedDateTime other) → bool - Checks if this date-time is before another.
-
isEqual(
ZonedDateTime other) → bool - Checks if this date-time represents the same instant as another.
-
minus(
Duration duration) → ZonedDateTime - Subtracts the specified Duration from this date-time.
-
minusDays(
int days) → ZonedDateTime - Subtracts the specified number of days.
-
minusHours(
int hours) → ZonedDateTime - Subtracts the specified number of hours.
-
minusMilliseconds(
int milliseconds) → ZonedDateTime - Subtracts the specified number of milliseconds.
-
minusMinutes(
int minutes) → ZonedDateTime - Subtracts the specified number of minutes.
-
minusMonths(
int months) → ZonedDateTime - Subtracts the specified number of months.
-
minusSeconds(
int seconds) → ZonedDateTime - Subtracts the specified number of seconds.
-
minusWeeks(
int weeks) → ZonedDateTime - Subtracts the specified number of weeks.
-
minusYears(
int years) → ZonedDateTime - Subtracts the specified number of years.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
plus(
Duration duration) → ZonedDateTime - Adds the specified Duration to this date-time.
-
plusDays(
int days) → ZonedDateTime - Adds the specified number of days.
-
plusHours(
int hours) → ZonedDateTime - Adds the specified number of hours.
-
plusMilliseconds(
int milliseconds) → ZonedDateTime - Adds the specified number of milliseconds.
-
plusMinutes(
int minutes) → ZonedDateTime - Adds the specified number of minutes.
-
plusMonths(
int months) → ZonedDateTime - Adds the specified number of months.
-
plusSeconds(
int seconds) → ZonedDateTime - Adds the specified number of seconds.
-
plusWeeks(
int weeks) → ZonedDateTime - Adds the specified number of weeks.
-
plusYears(
int years) → ZonedDateTime - Adds the specified number of years.
-
toDateTime(
) → DateTime - Converts to a standard Dart DateTime object.
-
toEpochMilli(
) → int - Gets the milliseconds since the Unix epoch (January 1, 1970 UTC).
-
toLocalDate(
) → LocalDate - Gets just the date part as a LocalDate.
-
toLocalTime(
) → LocalTime - Gets just the time part as a LocalTime.
-
toString(
) → String -
Returns the ISO 8601 string representation with timezone information.
override
-
toStringCompact(
) → String - Returns a compact string representation without the zone ID.
-
toUtc(
) → ZonedDateTime - Converts this date-time to UTC.
-
withZoneSameInstant(
ZoneId zone) → ZonedDateTime - Converts this date-time to the same instant in a different timezone.
-
withZoneSameLocal(
ZoneId zone) → ZonedDateTime - Creates a new ZonedDateTime with the same local date-time but different timezone.
Operators
-
operator ==(
Object other) → bool -
Checks equality with another object.
override