- java.lang.Object
-
- java.lang.Enum<AmPm>
-
- org.threeten.extra.AmPm
-
- All Implemented Interfaces:
Serializable
,Comparable<AmPm>
,TemporalAccessor
,TemporalAdjuster
public enum AmPm extends Enum<AmPm> implements TemporalAccessor, TemporalAdjuster
A half-day before or after midday, with the values 'AM' and 'PM'.AmPm
is an enum representing the half-day concepts of AM and PM. AM is defined as from 00:00 to 11:59, while PM is defined from 12:00 to 23:59.All date-time fields have an
int
value. Theint
value followsCalendar
, assigning 0 to AM and 1 to PM. It is recommended that applications use the enum rather than theint
value to ensure code clarity.Do not use
ordinal()
to obtain the numeric representation ofAmPm
. UsegetValue()
instead.This enum represents a common concept that is found in many calendar systems. As such, this enum may be used by any calendar system that has the AM/PM concept defined exactly equivalent to the ISO calendar system.
Implementation Requirements:
This is an immutable and thread-safe enum.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Temporal
adjustInto(Temporal temporal)
Adjusts the specified temporal object to have this am-pm value.static AmPm
from(TemporalAccessor temporal)
Obtains an instance ofAmPm
from a temporal object.int
get(TemporalField field)
Gets the value of the specified field from this am-pm as anint
.String
getDisplayName(TextStyle style, Locale locale)
Gets the textual representation, such as 'AM' or 'PM'.long
getLong(TemporalField field)
Gets the value of the specified field from this am-pm as along
.int
getValue()
Gets the AM/PMint
value.boolean
isSupported(TemporalField field)
Checks if the specified field is supported.static AmPm
of(int amPmValue)
Obtains an instance ofAmPm
from anint
value.static AmPm
ofHour(int hourOfDay)
Obtains an instance ofAmPm
from an hour-of-day.<R> R
query(TemporalQuery<R> query)
Queries this am-pm using the specified query.ValueRange
range(TemporalField field)
Gets the range of valid values for the specified field.static AmPm
valueOf(String name)
Returns the enum constant of this type with the specified name.static AmPm[]
values()
Returns an array containing the constants of this enum type, in the order they are declared.
-
-
-
Method Detail
-
values
public static AmPm[] values()
Returns an array containing the constants of this enum type, in the order they are declared. This method may be used to iterate over the constants as follows:for (AmPm c : AmPm.values()) System.out.println(c);
- Returns:
- an array containing the constants of this enum type, in the order they are declared
-
valueOf
public static AmPm valueOf(String name)
Returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)- Parameters:
name
- the name of the enum constant to be returned.- Returns:
- the enum constant with the specified name
- Throws:
IllegalArgumentException
- if this enum type has no constant with the specified nameNullPointerException
- if the argument is null
-
of
public static AmPm of(int amPmValue)
Obtains an instance ofAmPm
from anint
value.AmPm
is an enum representing before and after midday. This factory allows the enum to be obtained from theint
value. Theint
value followsCalendar
, assigning 0 to AM and 1 to PM.- Parameters:
amPmValue
- the AM/PM value to represent, from 0 (AM) to 1 (PM)- Returns:
- the AM/PM, not null
- Throws:
DateTimeException
- if the am-pm is invalid
-
ofHour
public static AmPm ofHour(int hourOfDay)
Obtains an instance ofAmPm
from an hour-of-day.AmPm
is an enum representing before and after midday. This factory allows the enum to be obtained from the hour-of-day value, from 0 to 23.- Parameters:
hourOfDay
- the hour-of-day to extract from, from 0 to 23- Returns:
- the AM/PM, not null
- Throws:
DateTimeException
- if the hour-of-day is invalid
-
from
public static AmPm from(TemporalAccessor temporal)
Obtains an instance ofAmPm
from a temporal object.This obtains an am-pm based on the specified temporal. A
TemporalAccessor
represents an arbitrary set of date and time information, which this factory converts to an instance ofAmPm
.The conversion extracts the
AMPM_OF_DAY
field.This method matches the signature of the functional interface
TemporalQuery
allowing it to be used as a query via method reference,AmPm::from
.- Parameters:
temporal
- the temporal object to convert, not null- Returns:
- the AM/PM, not null
- Throws:
DateTimeException
- if unable to convert to aAmPm
-
getValue
public int getValue()
Gets the AM/PMint
value.The values are numbered following
Calendar
, assigning 0 to AM and 1 to PM.- Returns:
- the AM/PM value, from 0 (AM) to 1 (PM)
-
getDisplayName
public String getDisplayName(TextStyle style, Locale locale)
Gets the textual representation, such as 'AM' or 'PM'.This returns the textual name used to identify the am-pm, suitable for presentation to the user. The parameters control the style of the returned text and the locale.
If no textual mapping is found then the
numeric value
is returned.- Parameters:
style
- the length of the text required, not nulllocale
- the locale to use, not null- Returns:
- the text value of the am-pm, not null
-
isSupported
public boolean isSupported(TemporalField field)
Checks if the specified field is supported.This checks if this am-pm can be queried for the specified field. If false, then calling the
range
andget
methods will throw an exception.If the field is
AMPM_OF_DAY
then this method returns true. All otherChronoField
instances will return false.If the field is not a
ChronoField
, then the result of this method is obtained by invokingTemporalField.isSupportedBy(TemporalAccessor)
passingthis
as the argument. Whether the field is supported is determined by the field.- Specified by:
isSupported
in interfaceTemporalAccessor
- Parameters:
field
- the field to check, null returns false- Returns:
- true if the field is supported on this am-pm, false if not
-
range
public ValueRange range(TemporalField field)
Gets the range of valid values for the specified field.The range object expresses the minimum and maximum valid values for a field. This am-pm 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
AMPM_OF_DAY
then the range of the am-pm, from 0 to 1, will be returned. All otherChronoField
instances will throw anUnsupportedTemporalTypeException
.If the field is not a
ChronoField
, then the result of this method is obtained by invokingTemporalField.rangeRefinedBy(TemporalAccessor)
passingthis
as the argument. Whether the range can be obtained is determined by the field.- Specified by:
range
in interfaceTemporalAccessor
- Parameters:
field
- the field to query the range for, not null- Returns:
- the range of valid values for the field, not null
- Throws:
DateTimeException
- if the range for the field cannot be obtainedUnsupportedTemporalTypeException
- if the field is not supported
-
get
public int get(TemporalField field)
Gets the value of the specified field from this am-pm as anint
.This queries this am-pm for the value for 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
AMPM_OF_DAY
then the value of the am-pm, from 0 (AM) to 1 (PM), will be returned. All otherChronoField
instances will throw anUnsupportedTemporalTypeException
.If the field is not a
ChronoField
, then the result of this method is obtained by invokingTemporalField.getFrom(TemporalAccessor)
passingthis
as the argument. Whether the value can be obtained, and what the value represents, is determined by the field.- Specified by:
get
in interfaceTemporalAccessor
- Parameters:
field
- the field to get, not null- Returns:
- the value for the field, within the valid range of values
- Throws:
DateTimeException
- if a value for the field cannot be obtained or the value is outside the range of valid values for the fieldUnsupportedTemporalTypeException
- if the field is not supported or the range of values exceeds anint
ArithmeticException
- if numeric overflow occurs
-
getLong
public long getLong(TemporalField field)
Gets the value of the specified field from this am-pm as along
.This queries this am-pm for the value for 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
AMPM_OF_DAY
then the value of the am-pm, from 0 (AM) to 1 (PM), will be returned. All otherChronoField
instances will throw anUnsupportedTemporalTypeException
.If the field is not a
ChronoField
, then the result of this method is obtained by invokingTemporalField.getFrom(TemporalAccessor)
passingthis
as the argument. Whether the value can be obtained, and what the value represents, is determined by the field.- Specified by:
getLong
in interfaceTemporalAccessor
- Parameters:
field
- the field to get, not null- Returns:
- the value for the field
- Throws:
DateTimeException
- if a value for the field cannot be obtainedUnsupportedTemporalTypeException
- if the field is not supportedArithmeticException
- if numeric overflow occurs
-
query
public <R> R query(TemporalQuery<R> query)
Queries this am-pm using the specified query.This queries this am-pm using the specified query strategy object. The
TemporalQuery
object defines the logic to be used to obtain the result. Read the documentation of the query to understand what the result of this method will be.The result of this method is obtained by invoking the
TemporalQuery.queryFrom(TemporalAccessor)
method on the specified query passingthis
as the argument.- Specified by:
query
in interfaceTemporalAccessor
- Type Parameters:
R
- the type of the result- Parameters:
query
- the query to invoke, not null- Returns:
- the query result, null may be returned (defined by the query)
- Throws:
DateTimeException
- if unable to query (defined by the query)ArithmeticException
- if numeric overflow occurs (defined by the query)
-
adjustInto
public Temporal adjustInto(Temporal temporal)
Adjusts the specified temporal object to have this am-pm value.This returns a temporal object of the same observable type as the input with the am-pm changed to be the same as this.
The adjustment is equivalent to using
Temporal.with(TemporalField, long)
passingChronoField.AMPM_OF_DAY
as the field. Note that this adjusts forwards or backwards within a day.In most cases, it is clearer to reverse the calling pattern by using
Temporal.with(TemporalAdjuster)
:// these two lines are equivalent, but the second approach is recommended temporal = thisAmPm.adjustInto(temporal); temporal = temporal.with(thisAmPm);
This instance is immutable and unaffected by this method call.
- Specified by:
adjustInto
in interfaceTemporalAdjuster
- Parameters:
temporal
- the target object to be adjusted, not null- Returns:
- the adjusted object, not null
- Throws:
DateTimeException
- if unable to make the adjustmentArithmeticException
- if numeric overflow occurs
-
-