public final class HourOfDay extends java.lang.Object implements Calendrical, java.lang.Comparable<HourOfDay>, TimeAdjuster, CalendricalMatcher, java.io.Serializable
HourOfDay is an immutable time field that can only store a hour-of-day. It is a type-safe way of representing a hour-of-day in an application.
Static factory methods allow you to construct instances. The hour-of-day may be queried using getValue().
HourOfDay is immutable and thread-safe.
Modifier and Type | Method and Description |
---|---|
LocalTime |
adjustTime(LocalTime time)
Adjusts a time to have the the hour-of-day represented by this object,
returning a new time.
|
int |
compareTo(HourOfDay otherHourOfDay)
Compares this hour-of-day instance to another.
|
boolean |
equals(java.lang.Object otherHourOfDay)
Is this instance equal to that specified, evaluating the hour-of-day.
|
<T> T |
get(CalendricalRule<T> rule)
Gets the value of the specified calendrical rule.
|
AmPmOfDay |
getAmPm()
Gets whether it is AM or PM.
|
int |
getClockHourOfAmPm()
Gets the clock hour of AM or PM, from 1 to 12.
|
int |
getClockHourOfDay()
Gets the clock hour-of-day, from 1 to 24.
|
int |
getHourOfAmPm()
Gets the hour of AM or PM, from 0 to 11.
|
int |
getValue()
Gets the hour-of-day value.
|
int |
hashCode()
A hash code for the hour-of-day object.
|
static HourOfDay |
hourOfDay(AmPmOfDay amPm,
int hourOfAmPm)
Obtains an instance of
HourOfDay using am/pm. |
static HourOfDay |
hourOfDay(Calendrical calendrical)
Obtains an instance of
HourOfDay from a calendrical. |
static HourOfDay |
hourOfDay(int hourOfDay)
Obtains an instance of
HourOfDay . |
boolean |
matchesCalendrical(Calendrical calendrical)
Checks if the hour-of-day extracted from the calendrical matches this.
|
static DateTimeFieldRule<java.lang.Integer> |
rule()
Gets the rule that defines how the hour-of-day field operates.
|
java.lang.String |
toString()
A string describing the hour-of-day object.
|
public static DateTimeFieldRule<java.lang.Integer> rule()
The rule provides access to the minimum and maximum values, and a generic way to access values within a calendrical.
public static HourOfDay hourOfDay(int hourOfDay)
HourOfDay
.hourOfDay
- the hour-of-day to represent, from 0 to 23IllegalCalendarFieldValueException
- if the hourOfDay is invalidpublic static HourOfDay hourOfDay(AmPmOfDay amPm, int hourOfAmPm)
HourOfDay
using am/pm.amPm
- whether the hour is AM or PM, not nullhourOfAmPm
- the hour within AM/PM, from 0 to 11IllegalCalendarFieldValueException
- if the input is invalidpublic static HourOfDay hourOfDay(Calendrical calendrical)
HourOfDay
from a calendrical.
This can be used extract the hour-of-day value directly from any implementation
of Calendrical
, including those in other calendar systems.
calendrical
- the calendrical to extract from, not nullUnsupportedRuleException
- if the hour-of-day cannot be obtainedpublic <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 instance then
null
will be returned.
get
in interface Calendrical
rule
- the rule to use, not nullpublic int getValue()
public boolean matchesCalendrical(Calendrical calendrical)
matchesCalendrical
in interface CalendricalMatcher
calendrical
- the calendrical to match, not nullpublic LocalTime adjustTime(LocalTime time)
Only the hour-of-day field is adjusted in the result. The other time fields are unaffected.
This instance is immutable and unaffected by this method call.
adjustTime
in interface TimeAdjuster
time
- the time to be adjusted, not nullpublic AmPmOfDay getAmPm()
AM is defined as 00:00 to 11:59 inclusive.
PM is defined as 12:00 to 23:59 inclusive.
public int getHourOfAmPm()
This method returns the value from hourOfDay
modulo 12.
This is rarely used. The time as seen on clocks and watches is
returned from getClockHourOfAmPm()
.
The hour from 00:00 to 00:59 will return 0.
The hour from 01:00 to 01:59 will return 1.
The hour from 11:00 to 11:59 will return 11.
The hour from 12:00 to 12:59 will return 0.
The hour from 23:00 to 23:59 will return 11.
public int getClockHourOfAmPm()
This method returns values as you would commonly expect from a wall clock or watch.
The hour from 00:00 to 00:59 will return 12.
The hour from 01:00 to 01:59 will return 1.
The hour from 11:00 to 11:59 will return 11.
public int getClockHourOfDay()
This method returns the same as hourOfDay
, unless the
hour is 0, when this method returns 24.
The hour from 00:00 to 00:59 will return 24.
The hour from 01:00 to 01:59 will return 1.
The hour from 12:00 to 12:59 will return 12.
The hour from 23:00 to 23:59 will return 23.
public int compareTo(HourOfDay otherHourOfDay)
compareTo
in interface java.lang.Comparable<HourOfDay>
otherHourOfDay
- the other hour-of-day instance, not nulljava.lang.NullPointerException
- if otherHourOfDay is nullpublic boolean equals(java.lang.Object otherHourOfDay)
equals
in class java.lang.Object
otherHourOfDay
- the other hour-of-day instance, null returns falsepublic int hashCode()
hashCode
in class java.lang.Object
public java.lang.String toString()
toString
in class java.lang.Object