public final class LocalTime extends Object implements Calendrical, TimeProvider, CalendricalMatcher, TimeAdjuster, Comparable<LocalTime>, Serializable
10:15:30
.
LocalTime
is an immutable calendrical that represents a time, often
viewed as hour-minute-second.
This class stores all time fields, to a precision of nanoseconds.
It does not store or represent a date or time-zone. Thus, for example, the
value "13:45.30.123456789" can be stored in a LocalTime
.
LocalTime is immutable and thread-safe.
Modifier and Type | Class and Description |
---|---|
static class |
LocalTime.Overflow
The result of addition to a
LocalTime allowing the expression of
any overflow in days. |
Modifier and Type | Field and Description |
---|---|
static LocalTime |
MIDDAY
Constant for the local time of midday, 12:00.
|
static LocalTime |
MIDNIGHT
Constant for the local time of midnight, 00:00.
|
Modifier and Type | Method and Description |
---|---|
LocalTime |
adjustTime(LocalTime time)
Adjusts a time to have the value of this time.
|
OffsetTime |
atOffset(ZoneOffset offset)
Returns an offset time formed from this time and the specified offset.
|
int |
compareTo(LocalTime other)
Compares this
LocalTime to another time. |
boolean |
equals(Object other)
Checks if this
LocalTime is equal to the specified time. |
<T> T |
get(CalendricalRule<T> rule)
Gets the value of the specified calendrical rule.
|
ISOChronology |
getChronology()
Gets the chronology that this time uses, which is the ISO calendar system.
|
int |
getHourOfDay()
Gets the hour-of-day field.
|
int |
getMinuteOfHour()
Gets the minute-of-hour field.
|
int |
getNanoOfSecond()
Gets the nano-of-second field.
|
int |
getSecondOfMinute()
Gets the second-of-minute field.
|
int |
hashCode()
A hash code for this
LocalTime . |
boolean |
isAfter(LocalTime other)
Checks if this
LocalTime is after the specified time. |
boolean |
isBefore(LocalTime other)
Checks if this
LocalTime is before the specified time. |
boolean |
matches(CalendricalMatcher matcher)
Checks whether this
LocalTime matches the specified matcher. |
boolean |
matchesCalendrical(Calendrical calendrical)
Checks if the time extracted from the calendrical matches this.
|
LocalTime |
minus(Duration duration)
Returns a copy of this
LocalTime with the specified duration subtracted. |
LocalTime |
minus(PeriodProvider periodProvider)
Returns a copy of this
LocalTime with the specified period subtracted. |
LocalTime |
minusHours(long hours)
Returns a copy of this
LocalTime with the specified period in hours subtracted. |
LocalTime |
minusMinutes(long minutes)
Returns a copy of this
LocalTime with the specified period in minutes subtracted. |
LocalTime |
minusNanos(long nanos)
Returns a copy of this
LocalTime with the specified period in nanoseconds subtracted. |
LocalTime |
minusSeconds(long seconds)
Returns a copy of this
LocalTime with the specified period in seconds subtracted. |
LocalTime.Overflow |
minusWithOverflow(long hours,
long minutes,
long seconds,
long nanos)
Returns a copy of this
LocalTime with the specified period subtracted,
returning the new time with any overflow in days. |
static LocalTime |
now()
Obtains the current time from the system clock in the default time-zone.
|
static LocalTime |
now(Clock clock)
Obtains the current time from the specified clock.
|
static LocalTime |
of(int hourOfDay,
int minuteOfHour)
Obtains an instance of
LocalTime from an hour and minute. |
static LocalTime |
of(int hourOfDay,
int minuteOfHour,
int secondOfMinute)
Obtains an instance of
LocalTime from an hour, minute and second. |
static LocalTime |
of(int hourOfDay,
int minuteOfHour,
int secondOfMinute,
int nanoOfSecond)
Obtains an instance of
LocalTime from an hour, minute, second and nanosecond. |
static LocalTime |
of(TimeProvider timeProvider)
Obtains an instance of
LocalTime from a time provider. |
static LocalTime |
ofNanoOfDay(long nanoOfDay)
Obtains an instance of
LocalTime from a nanos-of-day value. |
static LocalTime |
ofSecondOfDay(long secondOfDay)
Obtains an instance of
LocalTime from a second-of-day value. |
static LocalTime |
ofSecondOfDay(long secondOfDay,
int nanoOfSecond)
Obtains an instance of
LocalTime from a second-of-day value, with
associated nanos of second. |
static LocalTime |
parse(String text)
Obtains an instance of
LocalTime from a text string such as 10:15 . |
static LocalTime |
parse(String text,
DateTimeFormatter formatter)
Obtains an instance of
LocalTime from a text string using a specific formatter. |
LocalTime |
plus(Duration duration)
Returns a copy of this
LocalTime with the specified duration added. |
LocalTime |
plus(PeriodProvider periodProvider)
Returns a copy of this
LocalTime with the specified period added. |
LocalTime |
plusHours(long hours)
Returns a copy of this
LocalTime with the specified period in hours added. |
LocalTime |
plusMinutes(long minutes)
Returns a copy of this
LocalTime with the specified period in minutes added. |
LocalTime |
plusNanos(long nanos)
Returns a copy of this
LocalTime with the specified period in nanoseconds added. |
LocalTime |
plusSeconds(long seconds)
Returns a copy of this
LocalTime with the specified period in seconds added. |
LocalTime.Overflow |
plusWithOverflow(long hours,
long minutes,
long seconds,
long nanos)
Returns a copy of this
LocalTime with the specified period added,
returning the new time with any overflow in days. |
static CalendricalRule<LocalTime> |
rule()
Gets the rule for
LocalTime . |
LocalTime |
toLocalTime()
Converts this time to a
LocalTime , trivially
returning this . |
long |
toNanoOfDay()
Extracts the time as nanos of day,
from
0 to 24 * 60 * 60 * 1,000,000,000 - 1 . |
LocalTime.Overflow |
toOverflow(long daysOverflow)
Returns this time wrapped as an days-overflow.
|
int |
toSecondOfDay()
Extracts the time as seconds of day,
from
0 to 24 * 60 * 60 - 1 . |
String |
toString()
Outputs this time as a
String , such as 10:15 . |
String |
toString(DateTimeFormatter formatter)
Outputs this time as a
String using the formatter. |
LocalTime |
with(TimeAdjuster adjuster)
Returns a copy of this
LocalTime with the time altered using the adjuster. |
LocalTime |
withHourOfDay(int hourOfDay)
Returns a copy of this
LocalTime with the hour-of-day value altered. |
LocalTime |
withMinuteOfHour(int minuteOfHour)
Returns a copy of this
LocalTime with the minute-of-hour value altered. |
LocalTime |
withNanoOfSecond(int nanoOfSecond)
Returns a copy of this
LocalTime with the nano-of-second value altered. |
LocalTime |
withSecondOfMinute(int secondOfMinute)
Returns a copy of this
LocalTime with the second-of-minute value altered. |
public static final LocalTime MIDNIGHT
public static final LocalTime MIDDAY
public static LocalTime now()
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.
public static LocalTime now(Clock 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
.
clock
- the clock to use, not nullpublic static LocalTime of(int hourOfDay, int minuteOfHour)
LocalTime
from an hour and minute.
The second and nanosecond fields will be set to zero by this factory method.
This factory may return a cached value, but applications must not rely on this.
hourOfDay
- the hour-of-day to represent, from 0 to 23minuteOfHour
- the minute-of-hour to represent, from 0 to 59IllegalCalendarFieldValueException
- if the value of any field is out of rangepublic static LocalTime of(int hourOfDay, int minuteOfHour, int secondOfMinute)
LocalTime
from an hour, minute and second.
The nanosecond field will be set to zero by this factory method.
This factory may return a cached value, but applications must not rely on this.
hourOfDay
- the hour-of-day to represent, from 0 to 23minuteOfHour
- the minute-of-hour to represent, from 0 to 59secondOfMinute
- the second-of-minute to represent, from 0 to 59IllegalCalendarFieldValueException
- if the value of any field is out of rangepublic static LocalTime of(int hourOfDay, int minuteOfHour, int secondOfMinute, int nanoOfSecond)
LocalTime
from an hour, minute, second and nanosecond.
This factory may return a cached value, but applications must not rely on this.
hourOfDay
- the hour-of-day to represent, from 0 to 23minuteOfHour
- the minute-of-hour to represent, from 0 to 59secondOfMinute
- the second-of-minute to represent, from 0 to 59nanoOfSecond
- the nano-of-second to represent, from 0 to 999,999,999IllegalCalendarFieldValueException
- if the value of any field is out of rangepublic static LocalTime of(TimeProvider timeProvider)
LocalTime
from a time provider.
The purpose of this method is to convert a TimeProvider
to a LocalTime
in the safest possible way. Specifically,
the means checking whether the input parameter is null and
whether the result of the provider is null.
This factory may return a cached value, but applications must not rely on this.
timeProvider
- the time provider to use, not nullpublic static LocalTime ofSecondOfDay(long secondOfDay)
LocalTime
from a second-of-day value.
This factory may return a cached value, but applications must not rely on this.
secondOfDay
- the second-of-day, from 0
to 24 * 60 * 60 - 1
IllegalCalendarFieldValueException
- if the second-of-day value is invalidpublic static LocalTime ofSecondOfDay(long secondOfDay, int nanoOfSecond)
LocalTime
from a second-of-day value, with
associated nanos of second.
This factory may return a cached value, but applications must not rely on this.
secondOfDay
- the second-of-day, from 0
to 24 * 60 * 60 - 1
nanoOfSecond
- the nano-of-second, from 0 to 999,999,999IllegalCalendarFieldValueException
- if the either input value is invalidpublic static LocalTime ofNanoOfDay(long nanoOfDay)
LocalTime
from a nanos-of-day value.
This factory may return a cached value, but applications must not rely on this.
nanoOfDay
- the nano of day, from 0
to 24 * 60 * 60 * 1,000,000,000 - 1
CalendricalException
- if the nanos of day value is invalidpublic static LocalTime parse(String text)
LocalTime
from a text string such as 10:15
.
The following formats are accepted in ASCII:
{Hour}:{Minute}
{Hour}:{Minute}:{Second}
{Hour}:{Minute}:{Second}.{NanosecondFraction}
The hour has 2 digits with values from 0 to 23. The minute has 2 digits with values from 0 to 59. The second has 2 digits with values from 0 to 59. The nanosecond fraction has from 1 to 9 digits with values from 0 to 999,999,999.
text
- the text to parse such as '10:15:30', not nullCalendricalException
- if the text cannot be parsedpublic static LocalTime parse(String text, DateTimeFormatter formatter)
LocalTime
from a text string using a specific formatter.
The text is parsed using the formatter, returning a time.
text
- the text to parse, not nullformatter
- the formatter to use, not nullUnsupportedOperationException
- if the formatter cannot parseCalendricalException
- if the text cannot be parsedpublic ISOChronology getChronology()
public <T> T get(CalendricalRule<T> rule)
This method queries the value of the specified calendrical rule.
If the value cannot be returned for the rule from this time then
null
will be returned.
get
in interface Calendrical
rule
- the rule to use, not nullpublic int getHourOfDay()
public int getMinuteOfHour()
public int getSecondOfMinute()
public int getNanoOfSecond()
public LocalTime with(TimeAdjuster adjuster)
LocalTime
with the time altered using the adjuster.
Adjusters can be used to alter the time in various ways. A simple adjuster might simply set the one of the fields, such as the hour field. A more complex adjuster might set the time to end of the working day.
This instance is immutable and unaffected by this method call.
adjuster
- the adjuster to use, not nullLocalTime
based on this time adjusted as necessary, never nullpublic LocalTime withHourOfDay(int hourOfDay)
LocalTime
with the hour-of-day value altered.
This instance is immutable and unaffected by this method call.
hourOfDay
- the hour-of-day to represent, from 0 to 23LocalTime
based on this time with the requested hour, never nullIllegalCalendarFieldValueException
- if the hour value is invalidpublic LocalTime withMinuteOfHour(int minuteOfHour)
LocalTime
with the minute-of-hour value altered.
This instance is immutable and unaffected by this method call.
minuteOfHour
- the minute-of-hour to represent, from 0 to 59LocalTime
based on this time with the requested minute, never nullIllegalCalendarFieldValueException
- if the minute value is invalidpublic LocalTime withSecondOfMinute(int secondOfMinute)
LocalTime
with the second-of-minute value altered.
This instance is immutable and unaffected by this method call.
secondOfMinute
- the second-of-minute to represent, from 0 to 59LocalTime
based on this time with the requested second, never nullIllegalCalendarFieldValueException
- if the second value is invalidpublic LocalTime withNanoOfSecond(int nanoOfSecond)
LocalTime
with the nano-of-second value altered.
This instance is immutable and unaffected by this method call.
nanoOfSecond
- the nano-of-second to represent, from 0 to 999,999,999LocalTime
based on this time with the requested nanosecond, never nullIllegalCalendarFieldValueException
- if the nanos value is invalidpublic LocalTime plus(PeriodProvider periodProvider)
LocalTime
with the specified period added.
This adds the specified period to this time, returning a new time. The calculation wraps around midnight and ignores any date-based ISO fields.
The period is interpreted using rules equivalent to Period.ofTimeFields(PeriodProvider)
.
Those rules ignore any date-based ISO fields, thus adding a date-based
period to this time will have no effect.
This instance is immutable and unaffected by this method call.
periodProvider
- the period to add, not nullLocalTime
based on this time with the period added, never nullCalendricalException
- if the specified period cannot be converted to a Period
ArithmeticException
- if the period overflows during conversion to hours/minutes/seconds/nanospublic LocalTime plus(Duration duration)
LocalTime
with the specified duration added.
This adds the specified duration to this time, returning a new time. The calculation wraps around midnight.
The calculation is equivalent to using plusSeconds(long)
and
plusNanos(long)
on the two parts of the duration.
This instance is immutable and unaffected by this method call.
duration
- the duration to add, not nullLocalTime
based on this time with the duration added, never nullpublic LocalTime plusHours(long hours)
LocalTime
with the specified period in hours added.
This adds the specified number of hours to this time, returning a new time. The calculation wraps around midnight.
This instance is immutable and unaffected by this method call.
hours
- the hours to add, may be negativeLocalTime
based on this time with the hours added, never nullpublic LocalTime plusMinutes(long minutes)
LocalTime
with the specified period in minutes added.
This adds the specified number of minutes to this time, returning a new time. The calculation wraps around midnight.
This instance is immutable and unaffected by this method call.
minutes
- the minutes to add, may be negativeLocalTime
based on this time with the minutes added, never nullpublic LocalTime plusSeconds(long seconds)
LocalTime
with the specified period in seconds added.
This adds the specified number of seconds to this time, returning a new time. The calculation wraps around midnight.
This instance is immutable and unaffected by this method call.
seconds
- the seconds to add, may be negativeLocalTime
based on this time with the seconds added, never nullpublic LocalTime plusNanos(long nanos)
LocalTime
with the specified period in nanoseconds added.
This adds the specified number of nanoseconds to this time, returning a new time. The calculation wraps around midnight.
This instance is immutable and unaffected by this method call.
nanos
- the nanos to add, may be negativeLocalTime
based on this time with the nanoseconds added, never nullpublic LocalTime.Overflow plusWithOverflow(long hours, long minutes, long seconds, long nanos)
LocalTime
with the specified period added,
returning the new time with any overflow in days.
This method returns an LocalTime.Overflow
instance with the result of the
addition and any overflow in days.
This instance is immutable and unaffected by this method call.
hours
- the hours to add, may be negativeminutes
- the minutes to add, may be negativeseconds
- the seconds to add, may be negativenanos
- the nanos to add, may be negativeOverflow
instance with the resulting time and overflow, never nullpublic LocalTime minus(PeriodProvider periodProvider)
LocalTime
with the specified period subtracted.
This subtracts the specified period from this time, returning a new time. The calculation wraps around midnight and ignores any date-based ISO fields.
The period is interpreted using rules equivalent to Period.ofTimeFields(PeriodProvider)
.
Those rules ignore any date-based ISO fields, thus adding a date-based
period to this time will have no effect.
This instance is immutable and unaffected by this method call.
periodProvider
- the period to subtract, not nullLocalTime
based on this time with the period subtracted, never nullCalendricalException
- if the specified period cannot be converted to a Period
ArithmeticException
- if the period overflows during conversion to hours/minutes/seconds/nanospublic LocalTime minus(Duration duration)
LocalTime
with the specified duration subtracted.
This subtracts the specified duration from this time, returning a new time. The calculation wraps around midnight.
The calculation is equivalent to using minusSeconds(long)
and
minusNanos(long)
on the two parts of the duration.
This instance is immutable and unaffected by this method call.
duration
- the duration to subtract, not nullLocalTime
based on this time with the duration subtracted, never nullpublic LocalTime minusHours(long hours)
LocalTime
with the specified period in hours subtracted.
This subtracts the specified number of hours from this time, returning a new time. The calculation wraps around midnight.
This instance is immutable and unaffected by this method call.
hours
- the hours to subtract, may be negativeLocalTime
based on this time with the hours subtracted, never nullpublic LocalTime minusMinutes(long minutes)
LocalTime
with the specified period in minutes subtracted.
This subtracts the specified number of minutes from this time, returning a new time. The calculation wraps around midnight.
This instance is immutable and unaffected by this method call.
minutes
- the minutes to subtract, may be negativeLocalTime
based on this time with the minutes subtracted, never nullpublic LocalTime minusSeconds(long seconds)
LocalTime
with the specified period in seconds subtracted.
This subtracts the specified number of seconds from this time, returning a new time. The calculation wraps around midnight.
This instance is immutable and unaffected by this method call.
seconds
- the seconds to subtract, may be negativeLocalTime
based on this time with the seconds subtracted, never nullpublic LocalTime minusNanos(long nanos)
LocalTime
with the specified period in nanoseconds subtracted.
This subtracts the specified number of nanoseconds from this time, returning a new time. The calculation wraps around midnight.
This instance is immutable and unaffected by this method call.
nanos
- the nanos to subtract, may be negativeLocalTime
based on this time with the nanoseconds subtracted, never nullpublic LocalTime.Overflow minusWithOverflow(long hours, long minutes, long seconds, long nanos)
LocalTime
with the specified period subtracted,
returning the new time with any overflow in days.
This method returns an LocalTime.Overflow
instance with the result of the
subtraction and any overflow in days.
This instance is immutable and unaffected by this method call.
hours
- the hours to subtract, may be negativeminutes
- the minutes to subtract, may be negativeseconds
- the seconds to subtract, may be negativenanos
- the nanos to subtract, may be negativeOverflow
instance with the resulting time and overflow, never nullpublic boolean matches(CalendricalMatcher matcher)
LocalTime
matches the specified matcher.
Matchers can be used to query the time. A simple matcher might simply compare one of the fields, such as the hour field. A more complex matcher might check if the time is the last second of the day.
matcher
- the matcher to use, not nullpublic boolean matchesCalendrical(Calendrical calendrical)
This method implements the CalendricalMatcher
interface.
It is intended that applications use matches(javax.time.calendar.CalendricalMatcher)
rather than this method.
matchesCalendrical
in interface CalendricalMatcher
calendrical
- the calendrical to match, not nullpublic LocalTime adjustTime(LocalTime time)
This method implements the TimeAdjuster
interface.
It is intended that applications use with(TimeAdjuster)
rather than this method.
adjustTime
in interface TimeAdjuster
time
- the time to be adjusted, not nullpublic OffsetTime atOffset(ZoneOffset offset)
This merges the two objects - this
and the specified offset -
to form an instance of OffsetTime
.
This instance is immutable and unaffected by this method call.
offset
- the offset to use, not nullpublic LocalTime toLocalTime()
LocalTime
, trivially
returning this
.toLocalTime
in interface TimeProvider
this
, never nullpublic LocalTime.Overflow toOverflow(long daysOverflow)
This method will generally only be needed by those writing low-level date and time code that handles days-overflow. An overflow happens when adding or subtracting to a time and the result overflows the range of a time. The number of days later (or earlier) of the result is recorded in the overflow.
daysOverflow
- the number of days to storepublic int toSecondOfDay()
0
to 24 * 60 * 60 - 1
.public long toNanoOfDay()
0
to 24 * 60 * 60 * 1,000,000,000 - 1
.public int compareTo(LocalTime other)
LocalTime
to another time.
The comparison is based on the time-line position of the times within a day.
compareTo
in interface Comparable<LocalTime>
other
- the other time to compare to, not nullNullPointerException
- if other
is nullpublic boolean isAfter(LocalTime other)
LocalTime
is after the specified time.
The comparison is based on the time-line position of the time within a day.
other
- the other time to compare to, not nullNullPointerException
- if other
is nullpublic boolean isBefore(LocalTime other)
LocalTime
is before the specified time.
The comparison is based on the time-line position of the time within a day.
other
- the other time to compare to, not nullNullPointerException
- if other
is nullpublic boolean equals(Object other)
LocalTime
is equal to the specified time.
The comparison is based on the time-line position of the time within a day.
public int hashCode()
LocalTime
.public String toString()
String
, such as 10:15
.
The output will be one of the following formats:
HH:mm
HH:mm:ss
HH:mm:ssfnnn
HH:mm:ssfnnnnnn
HH:mm:ssfnnnnnnnnn
public String toString(DateTimeFormatter formatter)
String
using the formatter.formatter
- the formatter to use, not nullUnsupportedOperationException
- if the formatter cannot printCalendricalPrintException
- if an error occurs during printingpublic static CalendricalRule<LocalTime> rule()
LocalTime
.Copyright © 2014. All rights reserved.