public class LocaleManager extends Object implements FormatConstants
ResourceManager
s,
and provides convenience methods for formatting various types of data
according to the rules of the current locale.
In the case of decimal-based values such as percentages and currency amounts, if the corresponding parse fails, this class will resort to parsing the string as a generic decimal value, throwing an exception if that also fails. This allows for the parsing of values that are formatted specifically or generically. For example, the currency value $49.55 will parse to the decimal value 49.55 whether the source string is "$49.55" or "49.55".
Modifier and Type | Field and Description |
---|---|
private static Collator |
collator |
private NumberFormat |
currencyFormat |
private DateFormat[] |
dateFormat |
private DateFormatSymbols |
dateFormatSymbols |
private DateFormat[] |
dateTimeFormat |
private NumberFormat |
decimalFormat |
static int |
DEFAULT_CURRENCY_DECIMALS
The default number of decimal digits to retain when formatting
currency values.
|
static int |
DEFAULT_NUMBER_DECIMALS
The default number of decimal digits to retain when formatting
numeric values.
|
static int |
DEFAULT_PERCENTAGE_DECIMALS
The default number of decimal digits to retain when formatting
percentage values.
|
private static LocaleManager |
defaultLocaleManager |
private NumberFormat |
integerFormat |
private int[] |
lengthTypes |
private NumberFormat |
percentFormat |
private static ParsePosition |
pos |
private Vector |
resourceManagers |
private DateFormat[] |
timeFormat |
CURRENCY_FORMAT, DATE_FORMAT, DATE_TIME_FORMAT, DECIMAL_FORMAT, INTEGER_FORMAT, LENGTH_MASK, LONG, MEDIUM, PERCENTAGE_FORMAT, SHORT, TIME_FORMAT, TYPE_MASK
Constructor and Description |
---|
LocaleManager()
Construct a new
LocaleManager . |
LocaleManager(ResourceManager resourceManager)
Construct a new
LocaleManager . |
Modifier and Type | Method and Description |
---|---|
void |
addResourceManager(ResourceManager manager)
Register a
ResourceManager with the locale manager. |
String |
formatCurrency(double value)
Format a currency value according to the rules of the current locale,
with
DEFAULT_CURRENCY_DECIMALS decimal places retained, and
grouping turned off. |
String |
formatCurrency(double value,
int decimals)
Format a currency value according to the rules of the current locale,
with the specified number of decimal places retained, and grouping turned
off.
|
String |
formatCurrency(double value,
int decimals,
boolean grouping)
Format a currency value according to the rules of the current locale,
with the specified number of decimal places retained, and the specified
grouping policy.
|
String |
formatCurrency(float value)
Format a currency value according to the rules of the current locale,
with
DEFAULT_CURRENCY_DECIMALS decimal places retained, and
grouping turned off. |
String |
formatCurrency(float value,
int decimals)
Format a currency value according to the rules of the current locale,
with the specified number of decimal places retained, and grouping turned
off.
|
String |
formatCurrency(float value,
int decimals,
boolean grouping)
Format a currency value according to the rules of the current locale,
with the specified number of decimal places retained, and the specified
grouping policy.
|
String |
formatDate(Calendar date)
Format a date according to the rules of the current locale, with the
default (medium) format length.
|
String |
formatDate(Date date)
Format a date according to the rules of the current locale, with the
default (medium) format length.
|
String |
formatDate(Date date,
int type)
Format a date according to the rules of the current locale, with the
specified format length.
|
String |
formatDateTime(Calendar date)
Format a date and time according to the rules of the current locale,
with the default (medium) format length.
|
String |
formatDateTime(Date date)
Format a date and time according to the rules of the current locale,
with the default (medium) format length.
|
String |
formatDateTime(Date date,
int type)
Format a date and time according to the rules of the current locale,
with the specified format length.
|
String |
formatDecimal(double value)
Format a floating point value according to the rules of the current
locale, with
DEFAULT_NUMBER_DECIMALS decimal places
retained, and grouping turned off. |
String |
formatDecimal(double value,
int decimals)
Format a floating point value according to the rules of the current
locale, with the specified number of decimal places retained, and
grouping turned off.
|
String |
formatDecimal(double value,
int decimals,
boolean grouping)
Format a floating point value according to the rules of the current
locale, with the specified number of decimal places retained, and the
specified grouping policy.
|
String |
formatDecimal(float value)
Format a floating point value according to the rules of the current
locale, with
DEFAULT_NUMBER_DECIMALS decimal places
retained, and grouping turned off. |
String |
formatDecimal(float value,
int decimals)
Format a floating point value according to the rules of the current
locale, with the specified number of decimal places retained, and
grouping turned off.
|
String |
formatDecimal(float value,
int decimals,
boolean grouping)
Format a floating point value according to the rules of the current
locale, with the specified number of decimal places retained, and the
specified grouping policy.
|
String |
formatInteger(int value)
Format an integer value according to the rules of the current
locale, with grouping turned off.
|
String |
formatInteger(int value,
boolean grouping)
Format an integer value according to the rules of the current
locale, and the specified grouping policy.
|
String |
formatInteger(long value)
Format an integer value according to the rules of the current
locale, with grouping turned off.
|
String |
formatInteger(long value,
boolean grouping)
Format an integer value according to the rules of the current
locale, and the specified grouping policy.
|
String |
formatPercentage(double value)
Format a percentage value according to the rules of the current locale,
with
DEFAULT_PERCENTAGE_DECIMALS decimal places retained, and
grouping turned off. |
String |
formatPercentage(double value,
int decimals)
Format a percentage value according to the rules of the current locale,
with the specified number of decimal places retained, and grouping turned
off.
|
String |
formatPercentage(double value,
int decimals,
boolean grouping)
Format a percentage value according to the rules of the current locale,
with the specified number of decimal places retained, and the specified
grouping policy.
|
String |
formatPercentage(float value)
Format a percentage value according to the rules of the current locale,
with
DEFAULT_PERCENTAGE_DECIMALS decimal places retained, and
grouping turned off. |
String |
formatPercentage(float value,
int decimals)
Format a percentage value according to the rules of the current locale,
with the specified number of decimal places retained, and grouping turned
off.
|
String |
formatPercentage(float value,
int decimals,
boolean grouping)
Format a percentage value according to the rules of the current locale,
with the specified number of decimal places retained, and the specified
grouping policy.
|
String |
formatTime(Calendar date)
Format a time according to the rules of the current locale, with the
default (medium) format length.
|
String |
formatTime(Date date)
Format a time according to the rules of the current locale, with the
default (medium) format length.
|
String |
formatTime(Date date,
int type)
Format a time according to the rules of the current locale, with the
specified format length.
|
Collator |
getCollator()
Get an instance of the
Collator object for the current
locale. |
DateFormatSymbols |
getDateFormatSymbols()
Get an instance of the
DateFormatSymbols object for the
current locale. |
static LocaleManager |
getDefault()
Get a reference to the default
LocaleManager . |
Locale |
getLocale()
Get the default locale.
|
LocaleData |
getLocaleData(String name)
Retrieve the named resource bundle.
|
DateFormat |
getShortDateFormat()
Get a reference to the short date format object for the current locale.
|
double |
parseCurrency(String s)
Parse a currency value from a string using the rules of the current
locale.
|
Date |
parseDate(String s)
Parse a date value from a string using the rules of the current locale,
with the default (medium) format length.
|
Date |
parseDate(String s,
int type)
Parse a date value from a string using the rules of the current locale,
with the specified format length.
|
Date |
parseDateTime(String s)
Parse a date and time value from a string using the rules of the current
locale, with the default (medium) format length.
|
Date |
parseDateTime(String s,
int type)
Parse a date and time value from a string using the rules of the current
locale, with the specified format length.
|
double |
parseDecimal(String s)
Parse a numeric value from a string using the rules of the current
locale.
|
long |
parseInteger(String s)
Parse an integer value from a string using the rules of the current
locale.
|
double |
parsePercentage(String s)
Parse a percentage value from a string using the rules of the current
locale.
|
Date |
parseTime(String s)
Parse a time value from a string using the rules of the current locale,
with the default (medium) format length.
|
Date |
parseTime(String s,
int type)
Parse a time value from a string using the rules of the current locale,
with the specified format length.
|
void |
removeResourceManager(ResourceManager manager)
Unregister a
ResourceManager from the locale manager. |
void |
setLocale(Locale locale)
Set the default locale.
|
private void |
trapGarbage(String s) |
private static LocaleManager defaultLocaleManager
private NumberFormat currencyFormat
private NumberFormat percentFormat
private NumberFormat decimalFormat
private NumberFormat integerFormat
private Vector resourceManagers
private static ParsePosition pos
private DateFormat[] dateFormat
private DateFormat[] timeFormat
private DateFormat[] dateTimeFormat
private int[] lengthTypes
private DateFormatSymbols dateFormatSymbols
public static final int DEFAULT_CURRENCY_DECIMALS
public static final int DEFAULT_PERCENTAGE_DECIMALS
public static final int DEFAULT_NUMBER_DECIMALS
public LocaleManager(ResourceManager resourceManager)
LocaleManager
.resourceManager
- The ResourceManager
that will be
used to load resource bundles.public LocaleManager()
LocaleManager
.public Locale getLocale()
LocaleManager
.public void setLocale(Locale locale)
Locale.setDefault()
(though it does call this method itself).locale
- The new locale.public static LocaleManager getDefault()
LocaleManager
.LocaleManager
.public void addResourceManager(ResourceManager manager)
ResourceManager
with the locale manager. The
locale manager searches through all registered resource managers when
searching for a resource bundle. The Kiwi internal resource manager is
always registered.manager
- The ResourceManager
to register.public void removeResourceManager(ResourceManager manager)
ResourceManager
from the locale manager. The
Kiwi internal resource manager is always registered and cannot be removed.manager
- The ResourceManager
to unregister.public LocaleData getLocaleData(String name) throws ResourceNotFoundException
LocaleData
object representing the specified
resource bundle.java.util.ResourceNotFoundException
- If the specified bundle
could not be located by any of the registered resource managers.ResourceNotFoundException
public String formatDate(Calendar date)
date
- The date to format.public String formatDate(Date date)
date
- The date to format.public String formatDate(Date date, int type)
date
- The date to format.type
- The format length; one of the symbolic constants
SHORT
, MEDIUM
, or LONG
.public String formatTime(Calendar date)
date
- The date to format.public String formatTime(Date date)
date
- The date to format.public String formatTime(Date date, int type)
date
- The date to format.type
- The format length; one of the symbolic constants
SHORT
, MEDIUM
, or LONG
.public String formatDateTime(Calendar date)
date
- The date to format.public String formatDateTime(Date date)
date
- The date to format.public String formatDateTime(Date date, int type)
date
- The date to format.type
- The format length; one of the symbolic constants
SHORT
, MEDIUM
, or LONG
.public Date parseDate(String s) throws ParseException
s
- The string to parse.Date
object.ParseException
- If the value could not be parsed.public Date parseDate(String s, int type) throws ParseException
s
- The string to parse.type
- The format length; one of the symbolic constants
SHORT
, MEDIUM
, or LONG
.Date
object.ParseException
- If the value could not be parsed.public Date parseTime(String s) throws ParseException
s
- The string to parse.Date
object.ParseException
- If the value could not be parsed.public Date parseTime(String s, int type) throws ParseException
s
- The string to parse.type
- The format length; one of the symbolic constants
SHORT
, MEDIUM
, or LONG
.Date
object.ParseException
- If the value could not be parsed.public Date parseDateTime(String s) throws ParseException
s
- The string to parse.Date
object.ParseException
- If the value could not be parsed.public Date parseDateTime(String s, int type) throws ParseException
s
- The string to parse.type
- The format length; one of the symbolic constants
SHORT
, MEDIUM
, or LONG
.Date
object.ParseException
- If the value could not be parsed.public String formatCurrency(double value)
DEFAULT_CURRENCY_DECIMALS
decimal places retained, and
grouping turned off.value
- The currency value to format.public String formatCurrency(double value, int decimals)
value
- The currency value to format.decimals
- The number of decimal places to retain.public String formatCurrency(double value, int decimals, boolean grouping)
value
- The currency value to format.decimals
- The number of decimal places to retain.grouping
- A flag specifying whether grouping should be used.public String formatCurrency(float value)
DEFAULT_CURRENCY_DECIMALS
decimal places retained, and
grouping turned off.value
- The currency value to format.public String formatCurrency(float value, int decimals)
value
- The currency value to format.decimals
- The number of decimal places to retain.public String formatCurrency(float value, int decimals, boolean grouping)
value
- The currency value to format.decimals
- The number of decimal places to retain.grouping
- A flag specifying whether grouping should be used.public double parseCurrency(String s) throws ParseException
s
- The string to parse.ParseException
- If the value could not be parsed.public String formatPercentage(double value)
DEFAULT_PERCENTAGE_DECIMALS
decimal places retained, and
grouping turned off.value
- The percentage value to format.public String formatPercentage(double value, int decimals)
value
- The percentage value to format.decimals
- The number of decimal places to retain.public String formatPercentage(double value, int decimals, boolean grouping)
value
- The percentage value to format.decimals
- The number of decimal places to retain.grouping
- A flag specifying whether grouping should be used.public String formatPercentage(float value)
DEFAULT_PERCENTAGE_DECIMALS
decimal places retained, and
grouping turned off.value
- The percentage value to format.public String formatPercentage(float value, int decimals)
value
- The percentage value to format.decimals
- The number of decimal places to retain.public String formatPercentage(float value, int decimals, boolean grouping)
value
- The percentage value to format.decimals
- The number of decimal places to retain.grouping
- A flag specifying whether grouping should be used.public double parsePercentage(String s) throws ParseException
s
- The string to parse.ParseException
- If the value could not be parsed.public String formatInteger(int value)
value
- The integer value to format.public String formatInteger(long value)
value
- The integer value to format.public String formatInteger(int value, boolean grouping)
value
- The integer value to format.public String formatInteger(long value, boolean grouping)
value
- The integer value to format.public long parseInteger(String s) throws ParseException
s
- The string to parse.ParseException
- If the value could not be parsed.public String formatDecimal(double value)
DEFAULT_NUMBER_DECIMALS
decimal places
retained, and grouping turned off.value
- The floating point value to format.public String formatDecimal(double value, int decimals)
value
- The floating point value to format.decimals
- The number of decimal places to retain.public String formatDecimal(double value, int decimals, boolean grouping)
value
- The floating point value to format.decimals
- The number of decimal places to retain.grouping
- A flag specifying whether grouping should be used.public String formatDecimal(float value)
DEFAULT_NUMBER_DECIMALS
decimal places
retained, and grouping turned off.value
- The floating point value to format.public String formatDecimal(float value, int decimals)
value
- The floating point value to format.decimals
- The number of decimal places to retain.public String formatDecimal(float value, int decimals, boolean grouping)
value
- The floating point value to format.decimals
- The number of decimal places to retain.grouping
- A flag specifying whether grouping should be used.public double parseDecimal(String s) throws ParseException
s
- The string to parse.ParseException
- If the value could not be parsed.public DateFormatSymbols getDateFormatSymbols()
DateFormatSymbols
object for the
current locale.DateFormatSymbols
object.public Collator getCollator()
Collator
object for the current
locale.Collator
object.public DateFormat getShortDateFormat()
DateFormat
object.private void trapGarbage(String s) throws ParseException
ParseException
WebARTS Library Licensed Under the GNU - General Public License. Other Libraries licensed under their respective Open Source Licenses