T - generic type of chronological entitypublic final class ChronoFormatter<T extends ChronoEntity<T>> extends Object implements ChronoPrinter<T>, ChronoParser<T>
Represents a chronological format for the conversion between a chronological text and the chronological value of type T.
An instance can either be created via a Builder obtainable
by setUp(Class, Locale) or by some predefined format constants
in Iso8601Format which can be adjusted by some
with()-methods. Another way to create an instance are the
methods formatter(...) and localFormatter(...) in
the classes PlainDate, PlainTime and Moment.
| Modifier and Type | Class and Description |
|---|---|
static class |
ChronoFormatter.Builder<T extends ChronoEntity<T>>
Builder for creating a new
ChronoFormatter. |
| Modifier and Type | Method and Description |
|---|---|
boolean |
equals(Object obj)
Compares the chronologies, default attributes, default values and
the internal format structures.
|
String |
format(T formattable)
Prints given chronological entity as formatted text.
|
Chronology<T> |
getChronology()
Returns the associated chronology.
|
Attributes |
getDefaultAttributes()
Returns the default attributes which are active if they are not
overridden by sectional attributes.
|
Locale |
getLocale()
Returns the locale setting.
|
int |
hashCode() |
T |
parse(CharSequence text)
Interpretes given text as chronological entity starting
at the begin of text.
|
T |
parse(CharSequence text,
ParseLog status)
Interpretes given text as chronological entity starting
at the specified position in parse log.
|
T |
parse(CharSequence text,
ParseLog status,
AttributeQuery attributes)
Interpretes given text as chronological entity starting
at the specified position in parse log.
|
Set<ElementPosition> |
print(T formattable,
Appendable buffer,
AttributeQuery attributes)
Prints given chronological entity as formatted text and writes
the text into given buffer.
|
Set<ElementPosition> |
print(T formattable,
StringBuilder buffer)
Prints given chronological entity as formatted text and writes
the text into given buffer.
|
static <T extends ChronoEntity<T>> |
setUp(Class<T> type,
Locale locale)
Constructs a builder for creating formatters.
|
Format |
toFormat()
Converts this formatter into a traditional
java.text.Format-object. |
String |
toString()
For debugging purposes.
|
<A extends Enum<A>> |
with(AttributeKey<A> key,
A value)
Creates a copy of this formatter with given enum-attribute.
|
ChronoFormatter<T> |
with(AttributeKey<Boolean> key,
boolean value)
Creates a copy of this formatter with given boolean-attribute.
|
ChronoFormatter<T> |
with(AttributeKey<Character> key,
char value)
Creates a copy of this formatter with given char-attribute.
|
ChronoFormatter<T> |
with(AttributeKey<Integer> key,
int value)
Creates a copy of this formatter with given int-attribute.
|
ChronoFormatter<T> |
with(Attributes attributes)
Creates a copy of this formatter with given standard attributes.
|
ChronoFormatter<T> |
with(Locale locale)
Creates a copy of this formatter with given locale.
|
<V> ChronoFormatter<T> |
withDefault(ChronoElement<V> element,
V value)
Determines a default replacement value for given element.
|
ChronoFormatter<T> |
withStdTimezone()
Equivalent to
withTimezone(Timezone.ofSystem().getID()). |
ChronoFormatter<T> |
withTimezone(String tzid)
Equivalent to
withTimezone(Timezone.of(tzid).getID()). |
ChronoFormatter<T> |
withTimezone(TZID tzid)
Creates a copy of this formatter with given timezone id which
shall be used in formatting or parsing.
|
public Chronology<T> getChronology()
Returns the associated chronology.
public Locale getLocale()
Returns the locale setting.
If there is just a reference to ISO-8601 without any concrete
language then this method will yield Locale.ROOT.
null)public Attributes getDefaultAttributes()
Returns the default attributes which are active if they are not overridden by sectional attributes.
The default attributes can be adjusted by a suitable
with()-method. Following attributes are predefined:
Attributes.CALENDAR_TYPE |
dependent on associated chronology |
Attributes.LANGUAGE |
dependent on associated locale |
Attributes.DECIMAL_SEPARATOR |
dependent on associated locale |
Attributes.ZERO_DIGIT |
dependent on associated locale |
Attributes.LENIENCY |
Leniency.SMART |
Attributes.PARSE_CASE_INSENSITIVE |
true |
Attributes.PARSE_PARTIAL_COMPARE |
false |
Attributes.TEXT_WIDTH |
TextWidth.WIDE |
Attributes.OUTPUT_CONTEXT |
OutputContext.FORMAT |
Attributes.PAD_CHAR |
(SPACE) |
getChronology(),
getLocale()public String format(T formattable)
Prints given chronological entity as formatted text.
formattable - object to be formattedIllegalArgumentException - if given object is not formattablepublic Set<ElementPosition> print(T formattable, StringBuilder buffer)
Prints given chronological entity as formatted text and writes the text into given buffer.
Equiovalent to
print(formattable, buffer, getDefaultAttributes()).
formattable - object to be formattedbuffer - text output bufferIllegalArgumentException - if given object is not formattablepublic Set<ElementPosition> print(T formattable, Appendable buffer, AttributeQuery attributes) throws IOException
Prints given chronological entity as formatted text and writes the text into given buffer.
The given attributes cannot change the inner format structure (for example not change a localized weekmodel), but can override some format properties like language or certain text attributes for this run only.
print in interface ChronoPrinter<T extends ChronoEntity<T>>formattable - object to be formattedbuffer - text output bufferattributes - attributes for limited formatting controlIllegalArgumentException - if given object is not formattableIOException - if writing to buffer failspublic T parse(CharSequence text) throws ParseException
Interpretes given text as chronological entity starting at the begin of text.
text - text to be parsedIndexOutOfBoundsException - if the text is emptyParseException - if the text is not parseablepublic T parse(CharSequence text, ParseLog status)
Interpretes given text as chronological entity starting at the specified position in parse log.
Equivalent to parse(text, status, getDefaultAttributes()).
text - text to be parsedstatus - parser information (always as new instance)null if parsing does not workIndexOutOfBoundsException - if the start position is at end of
text or even behindpublic T parse(CharSequence text, ParseLog status, AttributeQuery attributes)
Interpretes given text as chronological entity starting at the specified position in parse log.
The given attributes cannot change the inner format structure (for example not change a localized weekmodel), but can override some format properties like expected language or certain text attributes for this run only.
parse in interface ChronoParser<T extends ChronoEntity<T>>text - text to be parsedstatus - parser information (always as new instance)attributes - attributes for limited parsing controlnull if parsing does not workIndexOutOfBoundsException - if the start position is at end of
text or even behindpublic ChronoFormatter<T> with(Locale locale)
Creates a copy of this formatter with given locale.
Note: Sectional attributes will never be overridden. Is the locale not changed then the method will simply return this instance. Otherwise the copy will contain given locale and also adjusts the associated numerical symbols:
If necessary all inner format elements which are locale-dependent
will also be adjusted (for example
Weekmodel.weekOfYear(). But fixed literals will
remain unchanged hence this method might not be suitable for all
languages.
locale - new language and country configurationpublic ChronoFormatter<T> withTimezone(TZID tzid)
Creates a copy of this formatter with given timezone id which shall be used in formatting or parsing.
The timezone is in most cases only relevant for the type
Moment. When formatting the timezone helps
to convert the UTC value into a zonal representation. When
parsing the timezone serves as replacement value if the formatted
text does not contain any timezone.
tzid - timezone idpublic ChronoFormatter<T> withTimezone(String tzid)
Equivalent to withTimezone(Timezone.of(tzid).getID()).
tzid - timezone idIllegalArgumentException - if given timezone cannot be loadedwithTimezone(TZID)public ChronoFormatter<T> withStdTimezone()
Equivalent to withTimezone(Timezone.ofSystem().getID()).
public <V> ChronoFormatter<T> withDefault(ChronoElement<V> element, V value)
Determines a default replacement value for given element.
Example:
ChronoFormatter<PlainDate> fmt =
PlainDate.localFormatter("MM-dd", PatternType.CLDR)
.withDefault(PlainDate.YEAR, 2012);
PlainDate date = fmt.parse("05-21");
System.out.println(date); // 2012-05-21
V - generic element value typeelement - chronological element to be updatedvalue - replacement value or null
if the default value shall be deregisteredpublic ChronoFormatter<T> with(AttributeKey<Boolean> key, boolean value)
Creates a copy of this formatter with given boolean-attribute.
Note: Sectional attributes cannot be overridden.
key - attribute keyvalue - attribute valuepublic ChronoFormatter<T> with(AttributeKey<Integer> key, int value)
Creates a copy of this formatter with given int-attribute.
Note: Sectional attributes cannot be overridden.
key - attribute keyvalue - attribute valuepublic ChronoFormatter<T> with(AttributeKey<Character> key, char value)
Creates a copy of this formatter with given char-attribute.
Note: Sectional attributes cannot be overridden.
key - attribute keyvalue - attribute valuepublic <A extends Enum<A>> ChronoFormatter<T> with(AttributeKey<A> key, A value)
Creates a copy of this formatter with given enum-attribute.
Note: Sectional attributes cannot be overridden.
key - attribute keyvalue - attribute valuepublic ChronoFormatter<T> with(Attributes attributes)
Creates a copy of this formatter with given standard attributes.
Note: Sectional attributes cannot be overridden.
attributes - new default attributespublic Format toFormat()
Converts this formatter into a traditional
java.text.Format-object.
The returned format object also supports attributed strings such
that all ChronoElement-structures are associated with field
attributes of type DateFormat.Field.
In ISO systems following mapping will be applied:
PlainTime.AM_PM_OF_DAY =>
DateFormat.Field.AM_PMPlainTime.CLOCK_HOUR_OF_AMPM =>
DateFormat.Field.HOUR1PlainTime.CLOCK_HOUR_OF_DAY =>
DateFormat.Field.HOUR_OF_DAY1PlainDate.DAY_OF_MONTH =>
DateFormat.Field.DAY_OF_MONTHPlainDate.DAY_OF_WEEK =>
DateFormat.Field.DAY_OF_WEEKPlainDate.DAY_OF_YEAR =>
DateFormat.Field.DAY_OF_YEARPlainTime.DIGITAL_HOUR_OF_AMPM =>
DateFormat.Field.HOUR1PlainTime.DIGITAL_HOUR_OF_DAY =>
DateFormat.Field.HOUR0PlainTime.MILLI_OF_SECOND =>
DateFormat.Field.MILLISECONDPlainTime.MINUTE_OF_HOUR =>
DateFormat.Field.MINUTEPlainDate.MONTH_AS_NUMBER =>
DateFormat.Field.MONTHPlainDate.MONTH_OF_YEAR =>
DateFormat.Field.MONTHPlainTime.SECOND_OF_MINUTE =>
DateFormat.Field.SECONDPlainDate.WEEKDAY_IN_MONTH =>
DateFormat.Field.DAY_OF_WEEK_IN_MONTHPlainDate.YEAR =>
DateFormat.Field.YEARWeekmodel.boundedWeekOfMonth() =>
DateFormat.Field.WEEK_OF_MONTHWeekmodel.boundedWeekOfYear() =>
DateFormat.Field.WEEK_OF_YEARWeekmodel.weekOfMonth() =>
DateFormat.Field.WEEK_OF_MONTHWeekmodel.weekOfYear() =>
DateFormat.Field.WEEK_OF_YEARWeekmodel.localDayOfWeek() =>
DateFormat.Field.DAY_OF_WEEKChronoEntity.getTimezone() =>
DateFormat.Field.TIME_ZONENote: The returned Format-object is not serializable.
java.text.Format-object which
delegates all formatting and parsing work to this instancepublic static <T extends ChronoEntity<T>> ChronoFormatter.Builder<T> setUp(Class<T> type, Locale locale)
Constructs a builder for creating formatters.
T - generic chronological type (subtype of ChronoEntity)type - reified chronological typelocale - format localeBuilder-instanceIllegalArgumentException - if given chronological type is not
formattable that is if no chronology can be derived from typeChronology.lookup(Class)public boolean equals(Object obj)
Compares the chronologies, default attributes, default values and the internal format structures.
Copyright © 2014. All rights reserved.