Class LocalTime
LocalTime is an immutable date-time object that represents a time, often viewed as hour-minute-second. Time is represented to nanosecond precision. For example, the value «13:45.30.123456789» can be stored in a LocalTime .
This class does not store or represent a date or time-zone. Instead, it is a description of the local time as seen on a wall clock. It cannot represent an instant on the time-line without additional information such as an offset or time-zone.
The ISO-8601 calendar system is the modern civil calendar system used today in most of the world. This API assumes that all calendar systems use the same representation, this class, for time-of-day.
This is a value-based class; programmers should treat instances that are equal as interchangeable and should not use instances for synchronization, or unpredictable behavior may occur. For example, in a future release, synchronization may fail. The equals method should be used for comparisons.
Field Summary
Method Summary
Methods declared in class java.lang.Object
Field Details
MIN
MAX
The maximum supported LocalTime , ’23:59:59.999999999′. This is the time just before midnight at the end of the day.
MIDNIGHT
NOON
Method Details
now
Obtains the current time from the system clock in the default time-zone. This will query the system clock in the default time-zone to obtain the current time. Using this method will prevent the ability to use an alternate clock for testing because the clock is hard-coded.
now
Obtains the current time from the system clock in the specified time-zone. This will query the system clock to obtain the current time. Specifying the time-zone avoids dependence on the default time-zone. Using this method will prevent the ability to use an alternate clock for testing because the clock is hard-coded.
now
Obtains the current time from the specified clock. This will query the specified clock to obtain the current time. Using this method allows the use of an alternate clock for testing. The alternate clock may be introduced using dependency injection .
of
Obtains an instance of LocalTime from an hour and minute. This returns a LocalTime with the specified hour and minute. The second and nanosecond fields will be set to zero.
of
Obtains an instance of LocalTime from an hour, minute and second. This returns a LocalTime with the specified hour, minute and second. The nanosecond field will be set to zero.
of
Obtains an instance of LocalTime from an hour, minute, second and nanosecond. This returns a LocalTime with the specified hour, minute, second and nanosecond.
ofInstant
Obtains an instance of LocalTime from an Instant and zone ID. This creates a local time based on the specified instant. First, the offset from UTC/Greenwich is obtained using the zone ID and instant, which is simple as there is only one valid offset for each instant. Then, the instant and offset are used to calculate the local time.
ofSecondOfDay
Obtains an instance of LocalTime from a second-of-day value. This returns a LocalTime with the specified second-of-day. The nanosecond field will be set to zero.
ofNanoOfDay
Obtains an instance of LocalTime from a nanos-of-day value. This returns a LocalTime with the specified nanosecond-of-day.
from
Obtains an instance of LocalTime from a temporal object. This obtains a local time based on the specified temporal. A TemporalAccessor represents an arbitrary set of date and time information, which this factory converts to an instance of LocalTime . The conversion uses the TemporalQueries.localTime() query, which relies on extracting the NANO_OF_DAY field. This method matches the signature of the functional interface TemporalQuery allowing it to be used as a query via method reference, LocalTime::from .
parse
Obtains an instance of LocalTime from a text string such as 10:15 . The string must represent a valid time and is parsed using DateTimeFormatter.ISO_LOCAL_TIME .
parse
Obtains an instance of LocalTime from a text string using a specific formatter. The text is parsed using the formatter, returning a time.
isSupported
- NANO_OF_SECOND
- NANO_OF_DAY
- MICRO_OF_SECOND
- MICRO_OF_DAY
- MILLI_OF_SECOND
- MILLI_OF_DAY
- SECOND_OF_MINUTE
- SECOND_OF_DAY
- MINUTE_OF_HOUR
- MINUTE_OF_DAY
- HOUR_OF_AMPM
- CLOCK_HOUR_OF_AMPM
- HOUR_OF_DAY
- CLOCK_HOUR_OF_DAY
- AMPM_OF_DAY
If the field is not a ChronoField , then the result of this method is obtained by invoking TemporalField.isSupportedBy(TemporalAccessor) passing this as the argument. Whether the field is supported is determined by the field.
isSupported
- NANOS
- MICROS
- MILLIS
- SECONDS
- MINUTES
- HOURS
- HALF_DAYS
If the unit is not a ChronoUnit , then the result of this method is obtained by invoking TemporalUnit.isSupportedBy(Temporal) passing this as the argument. Whether the unit is supported is determined by the unit.
range
Gets the range of valid values for the specified field. The range object expresses the minimum and maximum valid values for a field. This time is used to enhance the accuracy of the returned range. If it is not possible to return the range, because the field is not supported or for some other reason, an exception is thrown. If the field is a ChronoField then the query is implemented here. The supported fields will return appropriate range instances. All other ChronoField instances will throw an UnsupportedTemporalTypeException . If the field is not a ChronoField , then the result of this method is obtained by invoking TemporalField.rangeRefinedBy(TemporalAccessor) passing this as the argument. Whether the range can be obtained is determined by the field.
get
Gets the value of the specified field from this time as an int . This queries this time for the value of the specified field. The returned value will always be within the valid range of values for the field. If it is not possible to return the value, because the field is not supported or for some other reason, an exception is thrown. If the field is a ChronoField then the query is implemented here. The supported fields will return valid values based on this time, except NANO_OF_DAY and MICRO_OF_DAY which are too large to fit in an int and throw an UnsupportedTemporalTypeException . All other ChronoField instances will throw an UnsupportedTemporalTypeException . If the field is not a ChronoField , then the result of this method is obtained by invoking TemporalField.getFrom(TemporalAccessor) passing this as the argument. Whether the value can be obtained, and what the value represents, is determined by the field.
getLong
Gets the value of the specified field from this time as a long . This queries this time for the value of the specified field. If it is not possible to return the value, because the field is not supported or for some other reason, an exception is thrown. If the field is a ChronoField then the query is implemented here. The supported fields will return valid values based on this time. All other ChronoField instances will throw an UnsupportedTemporalTypeException . If the field is not a ChronoField , then the result of this method is obtained by invoking TemporalField.getFrom(TemporalAccessor) passing this as the argument. Whether the value can be obtained, and what the value represents, is determined by the field.