Format
is an abstract base class for formatting locale-sensitive
information such as dates, messages, numbers, and lists.
Format
defines the programming interface for formatting
locale-sensitive objects into String
s (the
format
method) and for parsing String
s back
into objects (the parseObject
method).
Generally, a format's parseObject
method must be able to parse
any string formatted by its format
method. However, there may
be exceptional cases where this is not possible. For example, a
format
method might create two adjacent integer numbers with
no separator in between, and in this case the parseObject
could
not tell which digits belong to which number.
The Java Platform provides specialized subclasses of Format
--
DateFormat
, MessageFormat
, NumberFormat
, and
ListFormat
--for formatting dates, messages, numbers, and lists
respectively.
Concrete subclasses must implement three methods:
format(Object obj, StringBuffer toAppendTo, FieldPosition pos)
formatToCharacterIterator(Object obj)
parseObject(String source, ParsePosition pos)
MessageFormat
.
Subclasses often also provide additional format
methods for
specific input types as well as parse
methods for specific
result types. Any parse
method that does not take a
ParsePosition
argument should throw ParseException
when no text in the required format is at the beginning of the input text.
Most subclasses will also implement the following factory methods:
getInstance
for getting a useful format object appropriate
for the current locale
getInstance(Locale)
for getting a useful format
object appropriate for the specified locale
getXxxxInstance
methods for more specialized control. For
example, the NumberFormat
class provides
getPercentInstance
and getCurrencyInstance
methods for getting specialized number formatters.
Subclasses of Format
that allow programmers to create objects
for locales (with getInstance(Locale)
for example)
must also implement the following class method:
public static Locale[] getAvailableLocales()
And finally subclasses may define a set of constants to identify the various
fields in the formatted output. These constants are used to create a FieldPosition
object which identifies what information is contained in the field and its
position in the formatted result. These constants should be named
item_FIELD
where item
identifies
the field. For examples of these constants, see ERA_FIELD
and its
friends in DateFormat
.
Formats are generally not synchronized. It is recommended to create separate format instances for each thread. If multiple threads access a format concurrently, it must be synchronized externally.
java.text.ParsePosition
, java.text.FieldPosition
, java.text.NumberFormat
, java.text.DateFormat
, java.text.MessageFormat
, java.text.ListFormat
Modifier and Type | Class and Description |
---|---|
public static class | Format.
Defines constants that are used as attribute keys in the
|
pack-priv static interface | Format.
FieldDelegate is notified by the various |
Modifier and Type | Field and Description |
---|---|
private static final long |
Access | Constructor and Description |
---|---|
protected |
Modifier and Type | Method and Description |
---|---|
public Object | Returns: a clone of this instance.Overrides java. Creates and returns a copy of this object. |
pack-priv AttributedCharacterIterator | Returns: AttributedCharacterIterator wrapping sString to create AttributedCharacterIterator from s)Creates an |
pack-priv AttributedCharacterIterator | Returns: AttributedCharacterIterator wrapping passed in AttributedCharacterIteratorsAttributedCharacterIterators used to create resulting
AttributedCharacterIterators iterators)Creates an |
pack-priv AttributedCharacterIterator | Returns: AttributedCharacterIterator wrapping argsString to create AttributedCharacterIterator from string, AttributedCharacterIterator.Key for AttributedCharacterIterator key,Value associated with key in AttributedCharacterIterator value)Returns an AttributedCharacterIterator with the String
|
pack-priv AttributedCharacterIterator | Returns: AttributedCharacterIterator wrapping argsInitial AttributedCharacterIterator to add arg to iterator, AttributedCharacterIterator.Key for AttributedCharacterIterator key,Value associated with key in AttributedCharacterIterator value)Creates an AttributedCharacterIterator with the contents of
|
public final String | |
public abstract StringBuffer | Returns: the string buffer passed in astoAppendTo ,
with formatted text appendedThe object to format obj, StringBuffer where the text is to be appended toAppendTo, FieldPosition A pos)FieldPosition identifying a field
in the formatted textFormats an object and appends the resulting text to a given string buffer. |
public AttributedCharacterIterator | Returns: AttributedCharacterIterator describing the formatted value.The object to format obj)Formats an Object producing an |
public abstract Object | Returns: AnObject parsed from the string. In case of
error, returns null.A source, ParsePosition String , part of which should be parsed.A pos)ParsePosition object with index and error
index information as described above.Parses text from a string to produce an object. |
public Object | Returns: AnObject parsed from the string.A source)String whose beginning should be parsed.Parses text from the beginning of the given string to produce an object. |
serialVersionUID | back to summary |
---|---|
private static final long serialVersionUID
|
Format | back to summary |
---|---|
protected Format() Sole constructor. (For invocation by subclass constructors, typically implicit.) |
clone | back to summary |
---|---|
public Object clone() Overrides java. Creates and returns a copy of this object.
|
createAttributedCharacterIterator | back to summary |
---|---|
pack-priv AttributedCharacterIterator createAttributedCharacterIterator(String s) Creates an
|
createAttributedCharacterIterator | back to summary |
---|---|
pack-priv AttributedCharacterIterator createAttributedCharacterIterator(AttributedCharacterIterator[] iterators) Creates an
|
createAttributedCharacterIterator | back to summary |
---|---|
pack-priv AttributedCharacterIterator createAttributedCharacterIterator(String string, AttributedCharacterIterator. Returns an AttributedCharacterIterator with the String
|
createAttributedCharacterIterator | back to summary |
---|---|
pack-priv AttributedCharacterIterator createAttributedCharacterIterator(AttributedCharacterIterator iterator, AttributedCharacterIterator. Creates an AttributedCharacterIterator with the contents of
|
format | back to summary |
---|---|
public final String format(Object obj) Formats an object to produce a string. This is equivalent to
|
format | back to summary |
---|---|
public abstract StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) Formats an object and appends the resulting text to a given string
buffer.
If the
|
formatToCharacterIterator | back to summary |
---|---|
public AttributedCharacterIterator formatToCharacterIterator(Object obj) Formats an Object producing an
Each attribute key of the AttributedCharacterIterator will be of type
The default implementation creates an
|
parseObject | back to summary |
---|---|
public abstract Object parseObject(String source, ParsePosition pos) Parses text from a string to produce an object.
The method attempts to parse text starting at the index given by
|
parseObject | back to summary |
---|---|
public Object parseObject(String source) throws ParseException Parses text from the beginning of the given string to produce an object. The method may not use the entire text of the given string.
|
AttributedCharacterIterator
returned
from Format.formatToCharacterIterator
and as
field identifiers in FieldPosition
.
Modifier and Type | Field and Description |
---|---|
private static final long |
Access | Constructor and Description |
---|---|
protected |
serialVersionUID | back to summary |
---|---|
private static final long serialVersionUID Hides java. |
Field | back to summary |
---|---|
protected Field(String name) Creates a Field with the specified name.
|
Format
implementations as they are formatting the Objects. This allows for
storage of the individual sections of the formatted String for
later use, such as in a FieldPosition
or for an
AttributedCharacterIterator
.
Delegates should NOT assume that the Format
will notify
the delegate of fields in any particular order.
Modifier and Type | Method and Description |
---|---|
public void | formatted(Format.
Identifies the field matched attr,Value associated with the field value, int Beginning location of the field, will be >= 0 start, int End of the field, will be >= start and <= buffer.length() end, StringBuffer Contains current formatted value, receiver should
NOT modify it. buffer)Notified when a particular region of the String is formatted. |
public void | formatted(int
Identifies the field by integer fieldID, Format.Identifies the field matched attr,Value associated with the field value, int Beginning location of the field, will be >= 0 start, int End of the field, will be >= start and <= buffer.length() end, StringBuffer Contains current formatted value, receiver should
NOT modify it. buffer)Notified when a particular region of the String is formatted. |
formatted | back to summary |
---|---|
public void formatted(Format. Notified when a particular region of the String is formatted. This
method will be invoked if there is no corresponding integer field id
matching
|
formatted | back to summary |
---|---|
public void formatted(int fieldID, Format. Notified when a particular region of the String is formatted.
|