LocalTime class
A value object representing a time of day without any date or timezone information.
LocalTime
is immutable and supports operations such as:
- Parsing from strings
- Formatting to strings
- Time arithmetic (addition/subtraction)
- Comparison and equality checks
This class is useful when working with:
- Scheduling systems
- Time pickers
- Representing specific times (like "08:30 AM") without a date context
Example
final time = LocalTime(14, 30); // 2:30 PM
final later = time.plusMinutes(45); // 3:15 PM
print(later); // 15:15:00
- Implemented types
Constructors
- LocalTime(int hour, int minute, [int second = 0, int millisecond = 0])
- Constructs a LocalTime instance from its components.
- LocalTime.fromDateTime(DateTime dateTime)
-
Constructs a LocalTime from a DateTime object by extracting its time portion.
factory
- LocalTime.fromMillisecondOfDay(int millisecondOfDay)
-
Creates a LocalTime from the number of milliseconds since midnight.
factory
- LocalTime.midnight()
-
Returns a LocalTime instance for midnight (00:00:00.000).
factory
- LocalTime.noon()
-
Returns a LocalTime instance for noon (12:00:00.000).
A value object representing a time of day without any date or timezone information.
factory
- LocalTime.now()
-
Creates a LocalTime representing the current system time (local clock).
factory
- LocalTime.parse(String timeString)
-
Parses a time string into a LocalTime instance.
factory
Properties
- hashCode → int
-
Returns a hash code based on hour, minute, second, and millisecond.
no setteroverride
- hour → int
-
The hour in 24-hour format. Must be between 0 and 23.
final
- millisecond → int
-
The millisecond of the second. Must be between 0 and 999.
final
- minute → int
-
The minute of the hour. Must be between 0 and 59.
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- second → int
-
The second of the minute. Must be between 0 and 59.
final
Methods
-
compareTo(
LocalTime other) → int -
Compares this time with another LocalTime.
override
-
isAfter(
LocalTime other) → bool -
Returns
true
if this time is later thanother
. -
isBefore(
LocalTime other) → bool -
Returns
true
if this time is earlier thanother
. -
isEqual(
LocalTime other) → bool -
Returns
true
if this time is equal toother
. -
minus(
Duration duration) → LocalTime -
Returns a new LocalTime with the given
duration
subtracted. -
minusHours(
int hours) → LocalTime -
Returns a new LocalTime with the given
duration
subtracted. -
minusMilliseconds(
int milliseconds) → LocalTime -
Returns a new LocalTime with the given
duration
subtracted. -
minusMinutes(
int minutes) → LocalTime -
Returns a new LocalTime with the given
duration
subtracted. -
minusSeconds(
int seconds) → LocalTime -
Returns a new LocalTime with the given
duration
subtracted. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
plus(
Duration duration) → LocalTime -
Returns a new LocalTime with the given
duration
added. -
plusHours(
int hours) → LocalTime -
Returns a new LocalTime with the given
duration
added. -
plusMilliseconds(
int milliseconds) → LocalTime -
Returns a new LocalTime with the given
duration
added. -
plusMinutes(
int minutes) → LocalTime -
Returns a new LocalTime with the given
duration
added. -
plusSeconds(
int seconds) → LocalTime -
Returns a new LocalTime with the given
duration
added. -
toMillisecondOfDay(
) → int - Returns the total number of milliseconds since midnight.
-
toSecondOfDay(
) → int - Returns the total number of seconds since midnight.
-
toString(
) → String -
Returns a string representation of the time in
HH:mm:ss
orHH:mm:ss.SSS
format.override
Operators
-
operator ==(
Object other) → bool -
Checks if another object is a LocalTime with the same values.
override