Top Description Inners Fields Constructors Methods
java.text

public abstract Class Format

extends Object
implements Serializable, Cloneable
Class Inheritance
All Implemented Interfaces
java.lang.Cloneable, java.io.Serializable
Known Direct Subclasses
java.text.ListFormat, java.text.MessageFormat, java.text.NumberFormat, java.time.format.DateTimeFormatter.ClassicFormat, java.text.DateFormat
Imports
java.io.Serializable

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 Strings (the format method) and for parsing Strings 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.

Subclassing

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:

  1. format(Object obj, StringBuffer toAppendTo, FieldPosition pos)
  2. formatToCharacterIterator(Object obj)
  3. parseObject(String source, ParsePosition pos)
These general methods allow polymorphic parsing and formatting of objects and are used, for example, by 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:

  1. getInstance for getting a useful format object appropriate for the current locale
  2. getInstance(Locale) for getting a useful format object appropriate for the specified locale
In addition, some subclasses may also implement other 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.

Synchronization

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.

Author
Mark Davis
Since
1.1
See Also
java.text.ParsePosition, java.text.FieldPosition, java.text.NumberFormat, java.text.DateFormat, java.text.MessageFormat, java.text.ListFormat

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
Format.Field

Defines constants that are used as attribute keys in the AttributedCharacterIterator returned from Format.formatToCharacterIterator and as field identifiers in FieldPosition.

pack-priv static interface
Format.FieldDelegate

FieldDelegate is notified by the various Format implementations as they are formatting the Objects.

Field Summary

Modifier and TypeField and Description
private static final long

Constructor Summary

AccessConstructor and Description
protected
Format()

Sole constructor.

Method Summary

Modifier and TypeMethod and Description
public Object

Returns:

a clone of this instance.
clone
()

Overrides java.lang.Object.clone.

Creates and returns a copy of this object.

pack-priv AttributedCharacterIterator

Returns:

AttributedCharacterIterator wrapping s
createAttributedCharacterIterator
(String
String to create AttributedCharacterIterator from
s
)

Creates an AttributedCharacterIterator for the String s.

pack-priv AttributedCharacterIterator

Returns:

AttributedCharacterIterator wrapping passed in AttributedCharacterIterators
createAttributedCharacterIterator
(AttributedCharacterIterator[]
AttributedCharacterIterators used to create resulting AttributedCharacterIterators
iterators
)

Creates an AttributedCharacterIterator containing the concatenated contents of the passed in AttributedCharacterIterators.

pack-priv AttributedCharacterIterator

Returns:

AttributedCharacterIterator wrapping args
createAttributedCharacterIterator
(String
String to create AttributedCharacterIterator from
string
,
AttributedCharacterIterator.Attribute
Key for AttributedCharacterIterator
key
,
Object
Value associated with key in AttributedCharacterIterator
value
)

Returns an AttributedCharacterIterator with the String string and additional key/value pair key, value.

pack-priv AttributedCharacterIterator

Returns:

AttributedCharacterIterator wrapping args
createAttributedCharacterIterator
(AttributedCharacterIterator
Initial AttributedCharacterIterator to add arg to
iterator
,
AttributedCharacterIterator.Attribute
Key for AttributedCharacterIterator
key
,
Object
Value associated with key in AttributedCharacterIterator
value
)

Creates an AttributedCharacterIterator with the contents of iterator and the additional attribute key value.

public final String

Returns:

Formatted string.
format
(Object
The object to format
obj
)

Formats an object to produce a string.

public abstract StringBuffer

Returns:

the string buffer passed in as toAppendTo, with formatted text appended
format
(Object
The object to format
obj
,
StringBuffer
where the text is to be appended
toAppendTo
,
FieldPosition
A FieldPosition identifying a field in the formatted text
pos
)

Formats an object and appends the resulting text to a given string buffer.

public AttributedCharacterIterator

Returns:

AttributedCharacterIterator describing the formatted value.
formatToCharacterIterator
(Object
The object to format
obj
)

Formats an Object producing an AttributedCharacterIterator.

public abstract Object

Returns:

An Object parsed from the string. In case of error, returns null.
parseObject
(String
A String, part of which should be parsed.
source
,
ParsePosition
A ParsePosition object with index and error index information as described above.
pos
)

Parses text from a string to produce an object.

public Object

Returns:

An Object parsed from the string.
parseObject
(String
A String whose beginning should be parsed.
source
)

Parses text from the beginning of the given string to produce an object.

Inherited from java.lang.Object:
equalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

serialVersionUIDback to summary
private static final long serialVersionUID
Annotations
@Serial

Constructor Detail

Formatback to summary
protected Format()

Sole constructor. (For invocation by subclass constructors, typically implicit.)

Method Detail

cloneback to summary
public Object clone()

Overrides java.lang.Object.clone.

Creates and returns a copy of this object.

Returns:Object

a clone of this instance.

createAttributedCharacterIteratorback to summary
pack-priv AttributedCharacterIterator createAttributedCharacterIterator(String s)

Creates an AttributedCharacterIterator for the String s.

Parameters
s:String

String to create AttributedCharacterIterator from

Returns:AttributedCharacterIterator

AttributedCharacterIterator wrapping s

createAttributedCharacterIteratorback to summary
pack-priv AttributedCharacterIterator createAttributedCharacterIterator(AttributedCharacterIterator[] iterators)

Creates an AttributedCharacterIterator containing the concatenated contents of the passed in AttributedCharacterIterators.

Parameters
iterators:AttributedCharacterIterator[]

AttributedCharacterIterators used to create resulting AttributedCharacterIterators

Returns:AttributedCharacterIterator

AttributedCharacterIterator wrapping passed in AttributedCharacterIterators

createAttributedCharacterIteratorback to summary
pack-priv AttributedCharacterIterator createAttributedCharacterIterator(String string, AttributedCharacterIterator.Attribute key, Object value)

Returns an AttributedCharacterIterator with the String string and additional key/value pair key, value.

Parameters
string:String

String to create AttributedCharacterIterator from

key:AttributedCharacterIterator.Attribute

Key for AttributedCharacterIterator

value:Object

Value associated with key in AttributedCharacterIterator

Returns:AttributedCharacterIterator

AttributedCharacterIterator wrapping args

createAttributedCharacterIteratorback to summary
pack-priv AttributedCharacterIterator createAttributedCharacterIterator(AttributedCharacterIterator iterator, AttributedCharacterIterator.Attribute key, Object value)

Creates an AttributedCharacterIterator with the contents of iterator and the additional attribute key value.

Parameters
iterator:AttributedCharacterIterator

Initial AttributedCharacterIterator to add arg to

key:AttributedCharacterIterator.Attribute

Key for AttributedCharacterIterator

value:Object

Value associated with key in AttributedCharacterIterator

Returns:AttributedCharacterIterator

AttributedCharacterIterator wrapping args

formatback to summary
public final String format(Object obj)

Formats an object to produce a string. This is equivalent to

format(obj, new StringBuffer(), new FieldPosition(0)).toString();
Parameters
obj:Object

The object to format

Returns:String

Formatted string.

Exceptions
IllegalArgumentException:
if the Format cannot format the given object
formatback 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 pos argument identifies a field used by the format, then its indices are set to the beginning and end of the first such field encountered.

Parameters
obj:Object

The object to format

toAppendTo:StringBuffer

where the text is to be appended

pos:FieldPosition

A FieldPosition identifying a field in the formatted text

Returns:StringBuffer

the string buffer passed in as toAppendTo, with formatted text appended

Exceptions
NullPointerException:
if toAppendTo or pos is null
IllegalArgumentException:
if the Format cannot format the given object
formatToCharacterIteratorback to summary
public AttributedCharacterIterator formatToCharacterIterator(Object obj)

Formats an Object producing an AttributedCharacterIterator. You can use the returned AttributedCharacterIterator to build the resulting String, as well as to determine information about the resulting String.

Each attribute key of the AttributedCharacterIterator will be of type Field. It is up to each Format implementation to define what the legal values are for each attribute in the AttributedCharacterIterator, but typically the attribute key is also used as the attribute value.

The default implementation creates an AttributedCharacterIterator with no attributes. Subclasses that support fields should override this and create an AttributedCharacterIterator with meaningful attributes.

Parameters
obj:Object

The object to format

Returns:AttributedCharacterIterator

AttributedCharacterIterator describing the formatted value.

Exceptions
NullPointerException:
if obj is null.
IllegalArgumentException:
when the Format cannot format the given object.
Since
1.4
parseObjectback 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 pos. If parsing succeeds, then the index of pos is updated to the index after the last character used (parsing does not necessarily use all characters up to the end of the string), and the parsed object is returned. The updated pos can be used to indicate the starting point for the next call to this method. If an error occurs, then the index of pos is not changed, the error index of pos is set to the index of the character where the error occurred, and null is returned.

Parameters
source:String

A String, part of which should be parsed.

pos:ParsePosition

A ParsePosition object with index and error index information as described above.

Returns:Object

An Object parsed from the string. In case of error, returns null.

Exceptions
NullPointerException:
if source or pos is null.
parseObjectback 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.

Parameters
source:String

A String whose beginning should be parsed.

Returns:Object

An Object parsed from the string.

Exceptions
ParseException:
if the beginning of the specified string cannot be parsed.
NullPointerException:
if source is null.
java.text back to summary

public Class Format.Field

extends Attribute
Class Inheritance
Known Direct Subclasses
java.text.MessageFormat.Field, java.text.NumberFormat.Field, java.text.DateFormat.Field

Defines constants that are used as attribute keys in the AttributedCharacterIterator returned from Format.formatToCharacterIterator and as field identifiers in FieldPosition.
Since
1.4

Field Summary

Modifier and TypeField and Description
private static final long
Inherited from java.text.AttributedCharacterIterator.Attribute:
INPUT_METHOD_SEGMENTLANGUAGEREADING

Constructor Summary

AccessConstructor and Description
protected
Field(String
Name of the attribute
name
)

Creates a Field with the specified name.

Method Summary

Inherited from java.text.AttributedCharacterIterator.Attribute:
equalsgetNamehashCodereadResolvetoString

Field Detail

serialVersionUIDback to summary
private static final long serialVersionUID

Hides java.text.AttributedCharacterIterator.Attribute.serialVersionUID.

Annotations
@Serial

Constructor Detail

Fieldback to summary
protected Field(String name)

Creates a Field with the specified name.

Parameters
name:String

Name of the attribute

java.text back to summary

pack-priv Interface Format.FieldDelegate

Known Direct Implementers
java.text.CharacterIteratorFieldDelegate, java.text.FieldPosition.Delegate

FieldDelegate is notified by the various 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.

See Also
FieldPosition#getFieldDelegate, CharacterIteratorFieldDelegate

Method Summary

Modifier and TypeMethod and Description
public void
formatted(Format.Field
Identifies the field matched
attr
,
Object
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.Field
Identifies the field matched
attr
,
Object
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.

Method Detail

formattedback to summary
public void formatted(Format.Field attr, Object value, int start, int end, StringBuffer buffer)

Notified when a particular region of the String is formatted. This method will be invoked if there is no corresponding integer field id matching attr.

Parameters
attr:Format.Field

Identifies the field matched

value:Object

Value associated with the field

start:int

Beginning location of the field, will be >= 0

end:int

End of the field, will be >= start and <= buffer.length()

buffer:StringBuffer

Contains current formatted value, receiver should NOT modify it.

formattedback to summary
public void formatted(int fieldID, Format.Field attr, Object value, int start, int end, StringBuffer buffer)

Notified when a particular region of the String is formatted.

Parameters
fieldID:int

Identifies the field by integer

attr:Format.Field

Identifies the field matched

value:Object

Value associated with the field

start:int

Beginning location of the field, will be >= 0

end:int

End of the field, will be >= start and <= buffer.length()

buffer:StringBuffer

Contains current formatted value, receiver should NOT modify it.