Top Description Fields Constructors Methods
org.threeten.extra

public final Enum AmPm

extends Enum<AmPm>
implements TemporalAccessor, TemporalAdjuster
Class Inheritance
All Implemented Interfaces
java.time.temporal.TemporalAdjuster, java.time.temporal.TemporalAccessor
Static Imports
java.time.temporal.ChronoField.AMPM_OF_DAY, .ChronoField.HOUR_OF_DAY, .ChronoUnit.HALF_DAYS

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. The int value follows Calendar, assigning 0 to AM and 1 to PM. It is recommended that applications use the enum rather than the int value to ensure code clarity.

Do not use ordinal() to obtain the numeric representation of AmPm. Use getValue() 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.

Field Summary

Modifier and TypeField and Description
public static final AmPm
AM

The singleton instance for the morning, AM - ante meridiem.

public static final AmPm
PM

The singleton instance for the afternoon, PM - post meridiem.

Constructor Summary

AccessConstructor and Description
private
AmPm()

Method Summary

Modifier and TypeMethod and Description
public Temporal

Returns:

the adjusted object, not null
adjustInto
(Temporal
the target object to be adjusted, not null
temporal
)

Implements java.time.temporal.TemporalAdjuster.adjustInto.

Adjusts the specified temporal object to have this am-pm value.

public static AmPm

Returns:

the AM/PM, not null
from
(TemporalAccessor
the temporal object to convert, not null
temporal
)

Obtains an instance of AmPm from a temporal object.

public int

Returns:

the value for the field, within the valid range of values
get
(TemporalField
the field to get, not null
field
)

Overrides default java.time.temporal.TemporalAccessor.get.

Gets the value of the specified field from this am-pm as an int.

public String

Returns:

the text value of the am-pm, not null
getDisplayName
(TextStyle
the length of the text required, not null
style
,
Locale
the locale to use, not null
locale
)

Gets the textual representation, such as 'AM' or 'PM'.

public long

Returns:

the value for the field
getLong
(TemporalField
the field to get, not null
field
)

Implements java.time.temporal.TemporalAccessor.getLong.

Gets the value of the specified field from this am-pm as a long.

public int

Returns:

the AM/PM value, from 0 (AM) to 1 (PM)
getValue
()

Gets the AM/PM int value.

public boolean

Returns:

true if the field is supported on this am-pm, false if not
isSupported
(TemporalField
the field to check, null returns false
field
)

Implements java.time.temporal.TemporalAccessor.isSupported.

Checks if the specified field is supported.

public static AmPm

Returns:

the AM/PM, not null
of
(int
the AM/PM value to represent, from 0 (AM) to 1 (PM)
amPmValue
)

Obtains an instance of AmPm from an int value.

public static AmPm

Returns:

the AM/PM, not null
ofHour
(int
the hour-of-day to extract from, from 0 to 23
hourOfDay
)

Obtains an instance of AmPm from an hour-of-day.

public <
the type of the result
R
>
R

Returns:

the query result, null may be returned (defined by the query)
query
(TemporalQuery<R>
the query to invoke, not null
query
)

Overrides default java.time.temporal.TemporalAccessor.query.

Queries this am-pm using the specified query.

public ValueRange

Returns:

the range of valid values for the field, not null
range
(TemporalField
the field to query the range for, not null
field
)

Overrides default java.time.temporal.TemporalAccessor.range.

Gets the range of valid values for the specified field.

public static AmPm
public static AmPm[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

AMback to summary
public static final AmPm AM

The singleton instance for the morning, AM - ante meridiem. This has the numeric value of 0.

PMback to summary
public static final AmPm PM

The singleton instance for the afternoon, PM - post meridiem. This has the numeric value of 1.

Constructor Detail

AmPmback to summary
private AmPm()

Method Detail

adjustIntoback to summary
public Temporal adjustInto(Temporal temporal)

Implements java.time.temporal.TemporalAdjuster.adjustInto.

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) passing ChronoField#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.

Parameters
temporal:Temporal

the target object to be adjusted, not null

Returns:Temporal

the adjusted object, not null

Annotations
@Override
Exceptions
DateTimeException:
if unable to make the adjustment
ArithmeticException:
if numeric overflow occurs
fromback to summary
public static AmPm from(TemporalAccessor temporal)

Obtains an instance of AmPm 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 of AmPm.

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:TemporalAccessor

the temporal object to convert, not null

Returns:AmPm

the AM/PM, not null

Exceptions
DateTimeException:
if unable to convert to a AmPm
getback to summary
public int get(TemporalField field)

Overrides default java.time.temporal.TemporalAccessor.get.

Gets the value of the specified field from this am-pm as an int.

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 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.

Parameters
field:TemporalField

the field to get, not null

Returns:int

the value for the field, within the valid range of values

Annotations
@Override
Exceptions
DateTimeException:
if a value for the field cannot be obtained or the value is outside the range of valid values for the field
UnsupportedTemporalTypeException:
if the field is not supported or the range of values exceeds an int
ArithmeticException:
if numeric overflow occurs
getDisplayNameback to summary
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:TextStyle

the length of the text required, not null

locale:Locale

the locale to use, not null

Returns:String

the text value of the am-pm, not null

getLongback to summary
public long getLong(TemporalField field)

Implements java.time.temporal.TemporalAccessor.getLong.

Gets the value of the specified field from this am-pm as a long.

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 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.

Parameters
field:TemporalField

the field to get, not null

Returns:long

the value for the field

Annotations
@Override
Exceptions
DateTimeException:
if a value for the field cannot be obtained
UnsupportedTemporalTypeException:
if the field is not supported
ArithmeticException:
if numeric overflow occurs
getValueback to summary
public int getValue()

Gets the AM/PM int value.

The values are numbered following Calendar, assigning 0 to AM and 1 to PM.

Returns:int

the AM/PM value, from 0 (AM) to 1 (PM)

isSupportedback to summary
public boolean isSupported(TemporalField field)

Implements java.time.temporal.TemporalAccessor.isSupported.

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 and get methods will throw an exception.

If the field is AMPM_OF_DAY then this method returns true. All other ChronoField instances will return false.

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.

Parameters
field:TemporalField

the field to check, null returns false

Returns:boolean

true if the field is supported on this am-pm, false if not

Annotations
@Override
ofback to summary
public static AmPm of(int amPmValue)

Obtains an instance of AmPm from an int value.

AmPm is an enum representing before and after midday. This factory allows the enum to be obtained from the int value. The int value follows Calendar, assigning 0 to AM and 1 to PM.

Parameters
amPmValue:int

the AM/PM value to represent, from 0 (AM) to 1 (PM)

Returns:AmPm

the AM/PM, not null

Exceptions
DateTimeException:
if the am-pm is invalid
ofHourback to summary
public static AmPm ofHour(int hourOfDay)

Obtains an instance of AmPm 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:int

the hour-of-day to extract from, from 0 to 23

Returns:AmPm

the AM/PM, not null

Exceptions
DateTimeException:
if the hour-of-day is invalid
queryback to summary
public <R> R query(TemporalQuery<R> query)

Overrides default java.time.temporal.TemporalAccessor.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 passing this as the argument.

Parameters
<R>
the type of the result
query:TemporalQuery<R>

the query to invoke, not null

Returns:R

the query result, null may be returned (defined by the query)

Annotations
@SuppressWarnings:unchecked
@Override
Exceptions
DateTimeException:
if unable to query (defined by the query)
ArithmeticException:
if numeric overflow occurs (defined by the query)
rangeback to summary
public ValueRange range(TemporalField field)

Overrides default java.time.temporal.TemporalAccessor.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 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 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.

Parameters
field:TemporalField

the field to query the range for, not null

Returns:ValueRange

the range of valid values for the field, not null

Annotations
@Override
Exceptions
DateTimeException:
if the range for the field cannot be obtained
UnsupportedTemporalTypeException:
if the field is not supported
valueOfback to summary
public static AmPm valueOf(String name)
valuesback to summary
public static AmPm[] values()