|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object javax.microedition.global.Formatter
Creates locale-specific renditions of data items. The following data items are handled:
It is also possible to construct messages that have ordered parameters.
Formatting methods MUST NOT return null
. If a formatting
method cannot perform the requested formatting, the return value SHOULD
be the empty string. This allows applications to check if the formatting
succeeded by examining the length of the return string.
The locale used by the formatter determines the formatting rules.
The selected locale is either the value of the
microedition.locale
system property or the locale
passed as a string parameter to the constructor.
If the selected locale is null
or the empty string,
formatting is locale-neutral (see below).
If the selected locale is non-null
and non-empty, but the
formatter implementation does not support it, the constructors throw an
UnsupportedLocaleException
.
If the selected locale is supported, the formatting is performed according to the rules of the locale. These rules are implementation-dependent and are not exposed to application programmers.
To find out which locales
are supported by the formatter implementation,
call the getSupportedLocales()
method.
Formatter MUST NOT perform hierarchical matching, since typically
there are enough differences between locales that formatting rules cannot
be reused. However, the implementation MAY internally map one locale to
another one. Typically this would involve cases where there are several
different variants for the same locale, such as de-DE-operator1
and de-DE-operator2
.
This class defines a number of constants that determine the extent and
style of the result when the formatDateTime
method is called.
The following styles are currently defined:
Mixed combinations (i.e. short date with long time and vice versa) are not supported. If both date and time components are present, their mutual order depends on the locale and is implementation-dependent.
Due to the limited amount of screen real estate available in many mobile devices, the results of date and time formatters SHOULD be compact, using numeric values whenever appropriate. However, an implementation MAY use textual elements for names of weekdays and months if appropriate.
For floating point numbers, the descriptions of the formatting results in the method documentation concern situations where the number is normal. If the number to be formatted is negative infinity, positive infinity, or NaN, any separators, decimals or currency symbols which would normally appear MUST NOT be present in the result. The symbols used for negative and positive infinity and NaN are implementation-specific. Implementations MAY use the appropriate Unicode character (U+221F INFINITY) if applicable.
Currency formatting MUST NOT perform any currency conversions based on exchange rates. Unless specified otherwise, the amount being formatted is assumed to be in the currency associated with the locale. If a different currency is desired, it can be selected by passing an ISO 4217 currency code (see References).
In locale-neutral formatting dates and times MUST conform to the Internet date/time
profile for the ISO 8601 standard as defined in RFC 3339.
Numbers and currencies MUST be formatted using Long.toString
or
Double.toString
, and currency amounts MUST NOT have a currency symbol
unless explicitly specified.
In percentage formatting the Unicode character U+0025 PERCENT SIGN MUST be appended to the
formatted number.
Locale-neutral formatting is provided because the MIDP specification
allows the microedition.locale
system property to be null
.
In such cases constructing a formatter without specifying a locale will use the locale-neutral values
to avoid an exceptional condition. (Normally that would construct a formatter for the
system default locale.) The application can also explicitly request
locale-neutral behaviour by passing null
as the locale.
ISO 4217:2001, Codes for the representation of currencies and funds. International Organization for Standardization, 2001.
Graham Klyne and Chris Newman, Date and Time on the Internet: Timestamps, RFC 3339. Available at http://www.ietf.org/rfc/rfc3339.txt.
Field Summary | |
static int |
DATE_LONG
Constant for the long date style. |
static int |
DATE_SHORT
Constant for the short date style. |
static int |
DATETIME_LONG
Constant for the long date and time style. |
static int |
DATETIME_SHORT
Constant for the short date and time style. |
static int |
TIME_LONG
Constant for the long time style. |
static int |
TIME_SHORT
Constant for the short time style. |
Constructor Summary | |
Formatter()
Constructs a formatter based on the current locale, which is the value of the microedition.locale system property. |
|
Formatter(java.lang.String locale)
Constructs a formatter for the specified locale. |
Method Summary | |
java.lang.String |
formatCurrency(double number)
Formats a currency amount using locale-specific rules. |
java.lang.String |
formatCurrency(double number,
java.lang.String currencyCode)
Formats a currency amount using the locale-specific rules but using the symbol of the specified currency. |
java.lang.String |
formatDateTime(java.util.Calendar dateTime,
int style)
Formats a date/time instance using locale-specific rules. |
static java.lang.String |
formatMessage(java.lang.String template,
java.lang.String[] params)
Formats a message with a variable number of ordered parameters. |
java.lang.String |
formatNumber(double number)
Formats a decimal number using locale-specific rules. |
java.lang.String |
formatNumber(double number,
int decimals)
Formats a decimal number using locale-specific rules, with the specified number of decimals. |
java.lang.String |
formatNumber(long number)
Formats an integer using locale-specific rules. |
java.lang.String |
formatPercentage(float value,
int decimals)
Formats a percentage with the specified number of decimals using locale-specific rules. |
java.lang.String |
formatPercentage(long value)
Formats an integral percentage value using locale-specific rules. |
java.lang.String |
getLocale()
Gets the locale of this formatter. |
static java.lang.String[] |
getSupportedLocales()
Gets the list of the locales supported by the formatter, as an array of valid microedition.locale values. |
Methods inherited from class java.lang.Object |
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
public static final int DATE_LONG
Constant for the long date style.
public static final int DATE_SHORT
Constant for the short date style.
public static final int DATETIME_LONG
Constant for the long date and time style.
public static final int DATETIME_SHORT
Constant for the short date and time style.
public static final int TIME_LONG
Constant for the long time style.
public static final int TIME_SHORT
Constant for the short time style.
Constructor Detail |
public Formatter()
Constructs a formatter based on the current locale, which is
the value of the microedition.locale
system property.
If the system property value is null
, locale
neutral formatting rules are used. If the implementation does
not support the current locale, an UnsupportedLocaleException
is
thrown.
UnsupportedLocaleException
- if the current locale is not
supported by the implementationpublic Formatter(java.lang.String locale)
Constructs a formatter for the specified locale.
If the specified locale is null
,
locale neutral formatting rules are used. If the implementation does
not support the specified locale, an UnsupportedLocaleException
is
thrown.
locale
- the locale identifier (null
to get locale-neutral behaviour)
UnsupportedLocaleException
- if the specified locale is not
supported by the implementation
java.lang.IllegalArgumentException
- if the specified locale
is not valid according to the MIDP 2.0 specificationMethod Detail |
public java.lang.String formatCurrency(double number)
Formats a currency amount using locale-specific rules. This method assumes that the amount is in the locale's currency. The result uses the locale-specific decimal separator and may include grouping separators. The number of decimals is also locale-specific.
This method MUST NOT perform any currency conversions based on exchange rates.
number
- the currency amount to format
public java.lang.String formatCurrency(double number, java.lang.String currencyCode)
Formats a currency amount using the locale-specific rules but using the symbol of the specified currency. The currency is specified using its ISO 4217 three-letter code, such as "USD", "EUR" or "GBP". If there is a currency symbol attached to the ISO 4217 code in the implementation, that symbol MUST be used instead of the locale's currency symbol. If the implementation does not provide a currency symbol for a given ISO 4217 code, the code MUST be used as such.
The result MAY include a locale-specific decimal separator and grouping separators. The number of decimals is also locale-specific.
This method MUST NOT perform any currency conversions based on exchange rates.
number
- the currency amount to formatcurrencyCode
- the ISO 4217 currency code to use
java.lang.IllegalArgumentException
- if currencyCode
is not a syntactically valid ISO 4217 code (three uppercase Latin letters)
java.lang.NullPointerException
- if currencyCode
is null
formatCurrency(double)
public java.lang.String formatDateTime(java.util.Calendar dateTime, int style)
Formats a date/time instance using locale-specific rules.
The style
parameter determines the extent
and style of the result.
dateTime
- the date and time to formatstyle
- the formatting style to use
java.lang.NullPointerException
- if dateTime
is null
java.lang.IllegalArgumentException
- if style
is not one of those defined in this classDATE_SHORT
,
DATE_LONG
,
TIME_SHORT
,
TIME_LONG
,
DATETIME_SHORT
,
DATETIME_LONG
,
Calendar
,
TimeZone
public static java.lang.String formatMessage(java.lang.String template, java.lang.String[] params)
Formats a message with a variable number of ordered parameters.
The template
argument is a string that may contain placeholders
for the parameters. The placeholders are of the form {n}
or {nn}
where n
is a digit 0...9. This allows parameters
numbered from 0 to 99. Leading zeros in parameter numbers below 10
are accepted. If there is only one parameter, it is specified as {0}
(or {00}
). The placeholders
MAY occur in arbitrary order, and they determine the final order
of the actual parameters in the result string. Not all parameters have
to be used in the template, but any parameter MAY occur an arbitrary number of times.
The parameters are passed as an array of strings. This method does not actually format dates, times, and other data; the items must be already formatted and the strings placed in the parameter array. This method then performs a simple string substitution.
If you need to include the {
character in the template, it MUST
be escaped using {{
, so that it is not mistaken for the initial
delimiter of a placeholder.
The length of the params
array determines the upper bound of the
{nn}
parameters in the template. If the template contains
references to parameter numbers higher than params.length - 1
,
this method throws an IllegalArgumentException
.
template
- the string templateparams
- the parameter strings to insert
java.lang.IllegalArgumentException
- if the template refers to
parameter numbers larger than params.length - 1
java.lang.NullPointerException
- if the template or the
parameter array is null
public java.lang.String formatNumber(double number)
Formats a decimal number using locale-specific rules. The result MAY include a locale-specific decimal separator and grouping separators.
number
- the number to format
public java.lang.String formatNumber(double number, int decimals)
Formats a decimal number using locale-specific rules, with the specified number of decimals. The result MAY include a locale-specific decimal separator and grouping separators.
The number of decimals MUST be between 1 and 15. The formatted result MUST have exactly the specified number of decimals, even if some of the trailing digits are zeroes.
number
- the number to formatdecimals
- the number of decimals to use (1 <= decimals <= 15)
java.lang.IllegalArgumentException
- if the number of decimals
is not between 1 and 15, inclusiveformatNumber(double)
public java.lang.String formatNumber(long number)
Formats an integer using locale-specific rules. The result MAY include grouping separators.
number
- the number to format
public java.lang.String formatPercentage(float value, int decimals)
Formats a percentage with the specified number of decimals using locale-specific rules. This method places the locale-specific percent sign at the correct position in relation to the number, with the appropriate amount of space (possibly none) between the sign and the number.
A percentage is expressed as a decimal number, with the value 0.0 interpreted as 0% and the value 1.0 as 100%. Percentages larger than 100% are expressed as values greater than 1.0. Negative percentages are allowed, and expressed as values smaller than 0.0. The percentage is rounded to the specified number of decimals.
The number of decimals MUST be between 1 and 15. The formatted result MUST have exactly the specified number of decimals, even if some of the trailing digits are zeroes.
value
- the percentage to format, expressed as a positive or
negative numberdecimals
- the number of decimals to use (1 <= decimals <= 15)
java.lang.IllegalArgumentException
- if the number of decimals
is not between 1 and 15, inclusivepublic java.lang.String formatPercentage(long value)
Formats an integral percentage value using locale-specific rules. This method places the locale-specific percent sign at the correct position in relation to the number, with the appropriate number of space (possibly none) between the sign and the number.
A percentage is expressed as an integer value. Negative percentages are allowed.
value
- the percentage to format
formatNumber(long)
public java.lang.String getLocale()
Gets the locale of this formatter. The return value is null
if this formatter is using locale-neutral formatting.
null
if using locale-neutral formattingpublic static java.lang.String[] getSupportedLocales()
Gets the list of the locales supported by the formatter,
as an array of valid microedition.locale
values.
If no locales are supported, the list MUST be empty. It MUST NOT
be null
. As the value null
is not technically a valid locale, but a special value to trigger
locale-neutral formatting, it MUST NOT appear in the array.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |