Class LocalTime
- java.lang.Object
-
- javax.time.calendar.LocalTime
-
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Comparable<LocalTime>
,Calendrical
,CalendricalMatcher
,TimeAdjuster
,TimeProvider
public final class LocalTime extends java.lang.Object implements Calendrical, TimeProvider, CalendricalMatcher, TimeAdjuster, java.lang.Comparable<LocalTime>, java.io.Serializable
A time without time-zone in the ISO-8601 calendar system, such as10: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.
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
LocalTime.Overflow
The result of addition to aLocalTime
allowing the expression of any overflow in days.(package private) static class
LocalTime.Rule
Rule implementation.
-
Field Summary
Fields Modifier and Type Field Description private byte
hour
The hour.private static LocalTime[]
HOURS
Constants for the local time of each hour.private static int
HOURS_PER_DAY
Hours per minute.static LocalTime
MIDDAY
Constant for the local time of midday, 12:00.static LocalTime
MIDNIGHT
Constant for the local time of midnight, 00:00.private byte
minute
The minute.private static int
MINUTES_PER_DAY
Minutes per day.private static int
MINUTES_PER_HOUR
Minutes per hour.private int
nano
The nanosecond.private static long
NANOS_PER_DAY
Nanos per day.private static long
NANOS_PER_HOUR
Nanos per hour.private static long
NANOS_PER_MINUTE
Nanos per minute.private static long
NANOS_PER_SECOND
Nanos per second.private byte
second
The second.private static int
SECONDS_PER_DAY
Seconds per day.private static int
SECONDS_PER_HOUR
Seconds per hour.private static int
SECONDS_PER_MINUTE
Seconds per minute.private static long
serialVersionUID
A serialization identifier for this class.
-
Constructor Summary
Constructors Modifier Constructor Description private
LocalTime(int hourOfDay, int minuteOfHour, int secondOfMinute, int nanoOfSecond)
Constructor, previously validated.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method 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 thisLocalTime
to another time.private static LocalTime
create(int hourOfDay, int minuteOfHour, int secondOfMinute, int nanoOfSecond)
Creates a local time from the hour, minute, second and nanosecond fields.boolean
equals(java.lang.Object other)
Checks if thisLocalTime
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 thisLocalTime
.boolean
isAfter(LocalTime other)
Checks if thisLocalTime
is after the specified time.boolean
isBefore(LocalTime other)
Checks if thisLocalTime
is before the specified time.boolean
matches(CalendricalMatcher matcher)
Checks whether thisLocalTime
matches the specified matcher.boolean
matchesCalendrical(Calendrical calendrical)
Checks if the time extracted from the calendrical matches this.LocalTime
minus(PeriodProvider periodProvider)
Returns a copy of thisLocalTime
with the specified period subtracted.LocalTime
minus(Duration duration)
Returns a copy of thisLocalTime
with the specified duration subtracted.LocalTime
minusHours(long hours)
Returns a copy of thisLocalTime
with the specified period in hours subtracted.LocalTime
minusMinutes(long minutes)
Returns a copy of thisLocalTime
with the specified period in minutes subtracted.LocalTime
minusNanos(long nanos)
Returns a copy of thisLocalTime
with the specified period in nanoseconds subtracted.LocalTime
minusSeconds(long seconds)
Returns a copy of thisLocalTime
with the specified period in seconds subtracted.LocalTime.Overflow
minusWithOverflow(long hours, long minutes, long seconds, long nanos)
Returns a copy of thisLocalTime
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 ofLocalTime
from an hour and minute.static LocalTime
of(int hourOfDay, int minuteOfHour, int secondOfMinute)
Obtains an instance ofLocalTime
from an hour, minute and second.static LocalTime
of(int hourOfDay, int minuteOfHour, int secondOfMinute, int nanoOfSecond)
Obtains an instance ofLocalTime
from an hour, minute, second and nanosecond.static LocalTime
of(TimeProvider timeProvider)
Obtains an instance ofLocalTime
from a time provider.static LocalTime
ofNanoOfDay(long nanoOfDay)
Obtains an instance ofLocalTime
from a nanos-of-day value.static LocalTime
ofSecondOfDay(long secondOfDay)
Obtains an instance ofLocalTime
from a second-of-day value.static LocalTime
ofSecondOfDay(long secondOfDay, int nanoOfSecond)
Obtains an instance ofLocalTime
from a second-of-day value, with associated nanos of second.static LocalTime
parse(java.lang.String text)
Obtains an instance ofLocalTime
from a text string such as10:15
.static LocalTime
parse(java.lang.String text, DateTimeFormatter formatter)
Obtains an instance ofLocalTime
from a text string using a specific formatter.LocalTime
plus(PeriodProvider periodProvider)
Returns a copy of thisLocalTime
with the specified period added.LocalTime
plus(Duration duration)
Returns a copy of thisLocalTime
with the specified duration added.LocalTime
plusHours(long hours)
Returns a copy of thisLocalTime
with the specified period in hours added.LocalTime
plusMinutes(long minutes)
Returns a copy of thisLocalTime
with the specified period in minutes added.LocalTime
plusNanos(long nanos)
Returns a copy of thisLocalTime
with the specified period in nanoseconds added.LocalTime
plusSeconds(long seconds)
Returns a copy of thisLocalTime
with the specified period in seconds added.LocalTime.Overflow
plusWithOverflow(long hours, long minutes, long seconds, long nanos)
Returns a copy of thisLocalTime
with the specified period added, returning the new time with any overflow in days.private LocalTime.Overflow
plusWithOverflow(long hours, long minutes, long seconds, long nanos, int sign)
Returns a copy of thisLocalTime
with the specified period added, returning the new time with any overflow in days.private java.lang.Object
readResolve()
Handle singletons on deserialization.static CalendricalRule<LocalTime>
rule()
Gets the rule forLocalTime
.LocalTime
toLocalTime()
Converts this time to aLocalTime
, trivially returningthis
.long
toNanoOfDay()
Extracts the time as nanos of day, from0
to24 * 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, from0
to24 * 60 * 60 - 1
.java.lang.String
toString()
Outputs this time as aString
, such as10:15
.java.lang.String
toString(DateTimeFormatter formatter)
Outputs this time as aString
using the formatter.LocalTime
with(TimeAdjuster adjuster)
Returns a copy of thisLocalTime
with the time altered using the adjuster.LocalTime
withHourOfDay(int hourOfDay)
Returns a copy of thisLocalTime
with the hour-of-day value altered.LocalTime
withMinuteOfHour(int minuteOfHour)
Returns a copy of thisLocalTime
with the minute-of-hour value altered.LocalTime
withNanoOfSecond(int nanoOfSecond)
Returns a copy of thisLocalTime
with the nano-of-second value altered.LocalTime
withSecondOfMinute(int secondOfMinute)
Returns a copy of thisLocalTime
with the second-of-minute value altered.
-
-
-
Field Detail
-
MIDNIGHT
public static final LocalTime MIDNIGHT
Constant for the local time of midnight, 00:00.
-
MIDDAY
public static final LocalTime MIDDAY
Constant for the local time of midday, 12:00.
-
HOURS
private static final LocalTime[] HOURS
Constants for the local time of each hour.
-
serialVersionUID
private static final long serialVersionUID
A serialization identifier for this class.- See Also:
- Constant Field Values
-
HOURS_PER_DAY
private static final int HOURS_PER_DAY
Hours per minute.- See Also:
- Constant Field Values
-
MINUTES_PER_HOUR
private static final int MINUTES_PER_HOUR
Minutes per hour.- See Also:
- Constant Field Values
-
MINUTES_PER_DAY
private static final int MINUTES_PER_DAY
Minutes per day.- See Also:
- Constant Field Values
-
SECONDS_PER_MINUTE
private static final int SECONDS_PER_MINUTE
Seconds per minute.- See Also:
- Constant Field Values
-
SECONDS_PER_HOUR
private static final int SECONDS_PER_HOUR
Seconds per hour.- See Also:
- Constant Field Values
-
SECONDS_PER_DAY
private static final int SECONDS_PER_DAY
Seconds per day.- See Also:
- Constant Field Values
-
NANOS_PER_SECOND
private static final long NANOS_PER_SECOND
Nanos per second.- See Also:
- Constant Field Values
-
NANOS_PER_MINUTE
private static final long NANOS_PER_MINUTE
Nanos per minute.- See Also:
- Constant Field Values
-
NANOS_PER_HOUR
private static final long NANOS_PER_HOUR
Nanos per hour.- See Also:
- Constant Field Values
-
NANOS_PER_DAY
private static final long NANOS_PER_DAY
Nanos per day.- See Also:
- Constant Field Values
-
hour
private final byte hour
The hour.
-
minute
private final byte minute
The minute.
-
second
private final byte second
The second.
-
nano
private final int nano
The nanosecond.
-
-
Constructor Detail
-
LocalTime
private LocalTime(int hourOfDay, int minuteOfHour, int secondOfMinute, int nanoOfSecond)
Constructor, previously validated.- Parameters:
hourOfDay
- the hour-of-day to represent, validated from 0 to 23minuteOfHour
- the minute-of-hour to represent, validated from 0 to 59secondOfMinute
- the second-of-minute to represent, validated from 0 to 59nanoOfSecond
- the nano-of-second to represent, validated from 0 to 999,999,999
-
-
Method Detail
-
now
public static LocalTime 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.
- Returns:
- the current time using the system clock, never null
-
now
public static LocalTime now(Clock clock)
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
.- Parameters:
clock
- the clock to use, not null- Returns:
- the current time, never null
-
of
public static LocalTime of(int hourOfDay, int minuteOfHour)
Obtains an instance ofLocalTime
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.
- Parameters:
hourOfDay
- the hour-of-day to represent, from 0 to 23minuteOfHour
- the minute-of-hour to represent, from 0 to 59- Returns:
- the local time, never null
- Throws:
IllegalCalendarFieldValueException
- if the value of any field is out of range
-
of
public static LocalTime of(int hourOfDay, int minuteOfHour, int secondOfMinute)
Obtains an instance ofLocalTime
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.
- Parameters:
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 59- Returns:
- the local time, never null
- Throws:
IllegalCalendarFieldValueException
- if the value of any field is out of range
-
of
public static LocalTime of(int hourOfDay, int minuteOfHour, int secondOfMinute, int nanoOfSecond)
Obtains an instance ofLocalTime
from an hour, minute, second and nanosecond.This factory may return a cached value, but applications must not rely on this.
- Parameters:
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,999- Returns:
- the local time, never null
- Throws:
IllegalCalendarFieldValueException
- if the value of any field is out of range
-
of
public static LocalTime of(TimeProvider timeProvider)
Obtains an instance ofLocalTime
from a time provider.The purpose of this method is to convert a
TimeProvider
to aLocalTime
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.
- Parameters:
timeProvider
- the time provider to use, not null- Returns:
- the local time, never null
-
ofSecondOfDay
public static LocalTime ofSecondOfDay(long secondOfDay)
Obtains an instance ofLocalTime
from a second-of-day value.This factory may return a cached value, but applications must not rely on this.
- Parameters:
secondOfDay
- the second-of-day, from0
to24 * 60 * 60 - 1
- Returns:
- the local time, never null
- Throws:
IllegalCalendarFieldValueException
- if the second-of-day value is invalid
-
ofSecondOfDay
public static LocalTime ofSecondOfDay(long secondOfDay, int nanoOfSecond)
Obtains an instance ofLocalTime
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.
- Parameters:
secondOfDay
- the second-of-day, from0
to24 * 60 * 60 - 1
nanoOfSecond
- the nano-of-second, from 0 to 999,999,999- Returns:
- the local time, never null
- Throws:
IllegalCalendarFieldValueException
- if the either input value is invalid
-
ofNanoOfDay
public static LocalTime ofNanoOfDay(long nanoOfDay)
Obtains an instance ofLocalTime
from a nanos-of-day value.This factory may return a cached value, but applications must not rely on this.
- Parameters:
nanoOfDay
- the nano of day, from0
to24 * 60 * 60 * 1,000,000,000 - 1
- Returns:
- the local time, never null
- Throws:
CalendricalException
- if the nanos of day value is invalid
-
parse
public static LocalTime parse(java.lang.String text)
Obtains an instance ofLocalTime
from a text string such as10: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.
- Parameters:
text
- the text to parse such as '10:15:30', not null- Returns:
- the parsed local time, never null
- Throws:
CalendricalException
- if the text cannot be parsed
-
parse
public static LocalTime parse(java.lang.String text, DateTimeFormatter formatter)
Obtains an instance ofLocalTime
from a text string using a specific formatter.The text is parsed using the formatter, returning a time.
- Parameters:
text
- the text to parse, not nullformatter
- the formatter to use, not null- Returns:
- the parsed local time, never null
- Throws:
java.lang.UnsupportedOperationException
- if the formatter cannot parseCalendricalException
- if the text cannot be parsed
-
create
private static LocalTime create(int hourOfDay, int minuteOfHour, int secondOfMinute, int nanoOfSecond)
Creates a local time from the hour, minute, second and nanosecond fields.This factory may return a cached value, but applications must not rely on this.
- Parameters:
hourOfDay
- the hour-of-day to represent, validated from 0 to 23minuteOfHour
- the minute-of-hour to represent, validated from 0 to 59secondOfMinute
- the second-of-minute to represent, validated from 0 to 59nanoOfSecond
- the nano-of-second to represent, validated from 0 to 999,999,999- Returns:
- the local time, never null
- Throws:
InvalidCalendarFieldException
- if the day-of-month is invalid for the month-year
-
readResolve
private java.lang.Object readResolve()
Handle singletons on deserialization.- Returns:
- the resolved object.
-
getChronology
public ISOChronology getChronology()
Gets the chronology that this time uses, which is the ISO calendar system.- Returns:
- the ISO chronology, never null
-
get
public <T> T get(CalendricalRule<T> rule)
Gets the value of the specified calendrical 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.- Specified by:
get
in interfaceCalendrical
- Parameters:
rule
- the rule to use, not null- Returns:
- the value for the rule, null if the value cannot be returned
-
getHourOfDay
public int getHourOfDay()
Gets the hour-of-day field.- Returns:
- the hour-of-day, from 0 to 23
-
getMinuteOfHour
public int getMinuteOfHour()
Gets the minute-of-hour field.- Returns:
- the minute-of-hour, from 0 to 59
-
getSecondOfMinute
public int getSecondOfMinute()
Gets the second-of-minute field.- Returns:
- the second-of-minute, from 0 to 59
-
getNanoOfSecond
public int getNanoOfSecond()
Gets the nano-of-second field.- Returns:
- the nano-of-second, from 0 to 999,999,999
-
with
public LocalTime with(TimeAdjuster adjuster)
Returns a copy of thisLocalTime
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.
- Parameters:
adjuster
- the adjuster to use, not null- Returns:
- a
LocalTime
based on this time adjusted as necessary, never null
-
withHourOfDay
public LocalTime withHourOfDay(int hourOfDay)
Returns a copy of thisLocalTime
with the hour-of-day value altered.This instance is immutable and unaffected by this method call.
- Parameters:
hourOfDay
- the hour-of-day to represent, from 0 to 23- Returns:
- a
LocalTime
based on this time with the requested hour, never null - Throws:
IllegalCalendarFieldValueException
- if the hour value is invalid
-
withMinuteOfHour
public LocalTime withMinuteOfHour(int minuteOfHour)
Returns a copy of thisLocalTime
with the minute-of-hour value altered.This instance is immutable and unaffected by this method call.
- Parameters:
minuteOfHour
- the minute-of-hour to represent, from 0 to 59- Returns:
- a
LocalTime
based on this time with the requested minute, never null - Throws:
IllegalCalendarFieldValueException
- if the minute value is invalid
-
withSecondOfMinute
public LocalTime withSecondOfMinute(int secondOfMinute)
Returns a copy of thisLocalTime
with the second-of-minute value altered.This instance is immutable and unaffected by this method call.
- Parameters:
secondOfMinute
- the second-of-minute to represent, from 0 to 59- Returns:
- a
LocalTime
based on this time with the requested second, never null - Throws:
IllegalCalendarFieldValueException
- if the second value is invalid
-
withNanoOfSecond
public LocalTime withNanoOfSecond(int nanoOfSecond)
Returns a copy of thisLocalTime
with the nano-of-second value altered.This instance is immutable and unaffected by this method call.
- Parameters:
nanoOfSecond
- the nano-of-second to represent, from 0 to 999,999,999- Returns:
- a
LocalTime
based on this time with the requested nanosecond, never null - Throws:
IllegalCalendarFieldValueException
- if the nanos value is invalid
-
plus
public LocalTime plus(PeriodProvider periodProvider)
Returns a copy of thisLocalTime
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.
- Parameters:
periodProvider
- the period to add, not null- Returns:
- a
LocalTime
based on this time with the period added, never null - Throws:
CalendricalException
- if the specified period cannot be converted to aPeriod
java.lang.ArithmeticException
- if the period overflows during conversion to hours/minutes/seconds/nanos
-
plus
public LocalTime plus(Duration duration)
Returns a copy of thisLocalTime
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)
andplusNanos(long)
on the two parts of the duration.This instance is immutable and unaffected by this method call.
- Parameters:
duration
- the duration to add, not null- Returns:
- a
LocalTime
based on this time with the duration added, never null
-
plusHours
public LocalTime plusHours(long hours)
Returns a copy of thisLocalTime
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.
- Parameters:
hours
- the hours to add, may be negative- Returns:
- a
LocalTime
based on this time with the hours added, never null
-
plusMinutes
public LocalTime plusMinutes(long minutes)
Returns a copy of thisLocalTime
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.
- Parameters:
minutes
- the minutes to add, may be negative- Returns:
- a
LocalTime
based on this time with the minutes added, never null
-
plusSeconds
public LocalTime plusSeconds(long seconds)
Returns a copy of thisLocalTime
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.
- Parameters:
seconds
- the seconds to add, may be negative- Returns:
- a
LocalTime
based on this time with the seconds added, never null
-
plusNanos
public LocalTime plusNanos(long nanos)
Returns a copy of thisLocalTime
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.
- Parameters:
nanos
- the nanos to add, may be negative- Returns:
- a
LocalTime
based on this time with the nanoseconds added, never null
-
plusWithOverflow
public LocalTime.Overflow plusWithOverflow(long hours, long minutes, long seconds, long nanos)
Returns a copy of thisLocalTime
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.
- Parameters:
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 negative- Returns:
- an
Overflow
instance with the resulting time and overflow, never null
-
plusWithOverflow
private LocalTime.Overflow plusWithOverflow(long hours, long minutes, long seconds, long nanos, int sign)
Returns a copy of thisLocalTime
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.
- Parameters:
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 negativesign
- the sign to determine add or subtract- Returns:
- an
Overflow
instance with the resulting time and overflow, never null
-
minus
public LocalTime minus(PeriodProvider periodProvider)
Returns a copy of thisLocalTime
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.
- Parameters:
periodProvider
- the period to subtract, not null- Returns:
- a
LocalTime
based on this time with the period subtracted, never null - Throws:
CalendricalException
- if the specified period cannot be converted to aPeriod
java.lang.ArithmeticException
- if the period overflows during conversion to hours/minutes/seconds/nanos
-
minus
public LocalTime minus(Duration duration)
Returns a copy of thisLocalTime
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)
andminusNanos(long)
on the two parts of the duration.This instance is immutable and unaffected by this method call.
- Parameters:
duration
- the duration to subtract, not null- Returns:
- a
LocalTime
based on this time with the duration subtracted, never null
-
minusHours
public LocalTime minusHours(long hours)
Returns a copy of thisLocalTime
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.
- Parameters:
hours
- the hours to subtract, may be negative- Returns:
- a
LocalTime
based on this time with the hours subtracted, never null
-
minusMinutes
public LocalTime minusMinutes(long minutes)
Returns a copy of thisLocalTime
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.
- Parameters:
minutes
- the minutes to subtract, may be negative- Returns:
- a
LocalTime
based on this time with the minutes subtracted, never null
-
minusSeconds
public LocalTime minusSeconds(long seconds)
Returns a copy of thisLocalTime
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.
- Parameters:
seconds
- the seconds to subtract, may be negative- Returns:
- a
LocalTime
based on this time with the seconds subtracted, never null
-
minusNanos
public LocalTime minusNanos(long nanos)
Returns a copy of thisLocalTime
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.
- Parameters:
nanos
- the nanos to subtract, may be negative- Returns:
- a
LocalTime
based on this time with the nanoseconds subtracted, never null
-
minusWithOverflow
public LocalTime.Overflow minusWithOverflow(long hours, long minutes, long seconds, long nanos)
Returns a copy of thisLocalTime
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.
- Parameters:
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 negative- Returns:
- an
Overflow
instance with the resulting time and overflow, never null
-
matches
public boolean matches(CalendricalMatcher matcher)
Checks whether thisLocalTime
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.
- Parameters:
matcher
- the matcher to use, not null- Returns:
- true if this time matches the matcher, false otherwise
-
matchesCalendrical
public boolean matchesCalendrical(Calendrical calendrical)
Checks if the time extracted from the calendrical matches this.This method implements the
CalendricalMatcher
interface. It is intended that applications usematches(javax.time.calendar.CalendricalMatcher)
rather than this method.- Specified by:
matchesCalendrical
in interfaceCalendricalMatcher
- Parameters:
calendrical
- the calendrical to match, not null- Returns:
- true if the calendrical matches, false otherwise
-
adjustTime
public LocalTime adjustTime(LocalTime time)
Adjusts a time to have the value of this time.This method implements the
TimeAdjuster
interface. It is intended that applications usewith(TimeAdjuster)
rather than this method.- Specified by:
adjustTime
in interfaceTimeAdjuster
- Parameters:
time
- the time to be adjusted, not null- Returns:
- the adjusted time, never null
-
atOffset
public OffsetTime atOffset(ZoneOffset offset)
Returns an offset time formed from this time and the specified offset.This merges the two objects -
this
and the specified offset - to form an instance ofOffsetTime
.This instance is immutable and unaffected by this method call.
- Parameters:
offset
- the offset to use, not null- Returns:
- the offset time formed from this time and the specified offset, never null
-
toLocalTime
public LocalTime toLocalTime()
Converts this time to aLocalTime
, trivially returningthis
.- Specified by:
toLocalTime
in interfaceTimeProvider
- Returns:
this
, never null
-
toOverflow
public LocalTime.Overflow toOverflow(long daysOverflow)
Returns this time wrapped as an days-overflow.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.
- Parameters:
daysOverflow
- the number of days to store- Returns:
- the days-overflow, never null
-
toSecondOfDay
public int toSecondOfDay()
Extracts the time as seconds of day, from0
to24 * 60 * 60 - 1
.- Returns:
- the second-of-day equivalent to this time
-
toNanoOfDay
public long toNanoOfDay()
Extracts the time as nanos of day, from0
to24 * 60 * 60 * 1,000,000,000 - 1
.- Returns:
- the nano of day equivalent to this time
-
compareTo
public int compareTo(LocalTime other)
Compares thisLocalTime
to another time.The comparison is based on the time-line position of the times within a day.
- Specified by:
compareTo
in interfacejava.lang.Comparable<LocalTime>
- Parameters:
other
- the other time to compare to, not null- Returns:
- the comparator value, negative if less, positive if greater
- Throws:
java.lang.NullPointerException
- ifother
is null
-
isAfter
public boolean isAfter(LocalTime other)
Checks if thisLocalTime
is after the specified time.The comparison is based on the time-line position of the time within a day.
- Parameters:
other
- the other time to compare to, not null- Returns:
- true if this is after the specified time
- Throws:
java.lang.NullPointerException
- ifother
is null
-
isBefore
public boolean isBefore(LocalTime other)
Checks if thisLocalTime
is before the specified time.The comparison is based on the time-line position of the time within a day.
- Parameters:
other
- the other time to compare to, not null- Returns:
- true if this point is before the specified time
- Throws:
java.lang.NullPointerException
- ifother
is null
-
equals
public boolean equals(java.lang.Object other)
Checks if thisLocalTime
is equal to the specified time.The comparison is based on the time-line position of the time within a day.
- Overrides:
equals
in classjava.lang.Object
- Parameters:
other
- the other time to compare to, null returns false- Returns:
- true if this point is equal to the specified time
-
hashCode
public int hashCode()
A hash code for thisLocalTime
.- Overrides:
hashCode
in classjava.lang.Object
- Returns:
- a suitable hash code
-
toString
public java.lang.String toString()
Outputs this time as aString
, such as10:15
.The output will be one of the following formats:
HH:mm
HH:mm:ss
HH:mm:ssfnnn
HH:mm:ssfnnnnnn
HH:mm:ssfnnnnnnnnn
- Overrides:
toString
in classjava.lang.Object
- Returns:
- the formatted time, never null
-
toString
public java.lang.String toString(DateTimeFormatter formatter)
Outputs this time as aString
using the formatter.- Parameters:
formatter
- the formatter to use, not null- Returns:
- the formatted time string, never null
- Throws:
java.lang.UnsupportedOperationException
- if the formatter cannot printCalendricalPrintException
- if an error occurs during printing
-
rule
public static CalendricalRule<LocalTime> rule()
Gets the rule forLocalTime
.- Returns:
- the rule for the time, never null
-
-