The class is designed so that there's never more than one
Currency
instance for any given currency. Therefore, there's
no public constructor. You obtain a Currency
instance using
the getInstance
methods.
Users can supersede the Java runtime currency data by means of the system
property java.util.currency.data
. If this system property is
defined then its value is the location of a properties file, the contents of
which are key/value pairs of the ISO 3166 country codes and the ISO 4217
currency data respectively. The value part consists of three ISO 4217 values
of a currency, i.e., an alphabetic code, a numeric code, and a minor unit.
Those three ISO 4217 values are separated by commas.
The lines which start with '#'s are considered comment lines. An optional UTC
timestamp may be specified per currency entry if users need to specify a
cutover date indicating when the new data comes into effect. The timestamp is
appended to the end of the currency properties and uses a comma as a separator.
If a UTC datestamp is present and valid, the JRE will only use the new currency
properties if the current UTC date is later than the date specified at class
loading time. The format of the timestamp must be of ISO 8601 format :
'yyyy-MM-dd'T'HH:mm:ss'
. For example,
#Sample currency properties
JP=JPZ,999,0
will supersede the currency data for Japan. If JPZ is one of the existing ISO 4217 currency code referred by other countries, the existing JPZ currency data is updated with the given numeric code and minor unit value.
#Sample currency properties with cutover date
JP=JPZ,999,0,2014-01-01T00:00:00
will supersede the currency data for Japan if Currency
class is loaded after
1st January 2014 00:00:00 GMT.
Where syntactically malformed entries are encountered, the entry is ignored
and the remainder of entries in file are processed. For instances where duplicate
country code entries exist, the behavior of the Currency information for that
Currency
is undefined and the remainder of entries in file are processed.
If multiple property entries with same currency code but different numeric code and/or minor unit are encountered, those entries are ignored and the remainder of entries in file are processed.
It is recommended to use java.
class while dealing
with Currency
or monetary values as it provides better handling of floating
point numbers and their operations.
java.math.BigDecimal
Modifier and Type | Class and Description |
---|---|
private static class | Currency.
Obtains a localized currency names from a CurrencyNameProvider implementation. |
private static class | |
private static class | |
private static class |
Modifier and Type | Field and Description |
---|---|
private static final int | |
private static HashSet | |
private static final int | |
private static final int | |
private final String | currencyCode
ISO 4217 currency code for this currency. |
pack-priv static int | |
private final transient int | defaultFractionDigits
Default fraction digits for this currency. |
private static final int | |
pack-priv static int | |
private static ConcurrentMap | |
private static final int | |
private static final int | |
pack-priv static int[] | |
private static final int | |
private static final int | |
private final transient int | numericCode
ISO 4217 numeric code for this currency. |
pack-priv static List | |
private static final long | |
private static final int | |
private static final int | |
private static final int | |
private static final int | |
private static final int | |
private static final int | |
private static final int | |
private static final int | |
pack-priv static List | |
private static final int | SYMBOL
Constants for retrieving localized names from the name providers. |
private static final int |
Access | Constructor and Description |
---|---|
private |
Modifier and Type | Method and Description |
---|---|
public static Set | Returns: the set of available currencies. If there is no currency available in the runtime, the returned set is empty.Gets the set of available currencies. |
public String | Returns: the ISO 4217 currency code of this currency.Gets the ISO 4217 currency code of this currency. |
public int | Returns: the default number of fraction digits used with this currencyGets the default number of fraction digits used with this currency. |
public String | Returns: the display name of this currency for the defaultDISPLAY localeGets the name that is suitable for displaying this currency for
the default |
public String | Returns: the display name of this currency for the specified localethe locale for which a display name for this currency is
needed locale)Gets the name that is suitable for displaying this currency for the specified locale. |
public static Currency | Returns: theCurrency instance for the given currency codethe ISO 4217 code of the currency currencyCode)Returns the |
private static Currency | |
public static Currency | Returns: theCurrency instance for the country of the given
locale, or null the locale for whose country a locale)Currency
instance is neededReturns the |
private static int | getMainTableEntry(char char1, char char2)
Gets the main table entry for the country whose country code consists of char1 and char2. |
public int | Returns: the ISO 4217 numeric code of this currencyReturns the ISO 4217 numeric code of this currency. |
public String | Returns: the 3 digit ISO 4217 numeric code of this currency as aString Returns the 3 digit ISO 4217 numeric code of this currency as a |
public String | |
public String | |
private static List | Returns: list of parsed property entriesproperties containing currency data props, Pattern regex pattern for the properties entry pattern)Parse currency data found in the properties file (that java.util.currency.data designates) to a List of CurrencyProperty instances. |
private static void | |
private static int[] | |
private static List | |
private Object | |
private static List | |
private static void | replaceCurrencyData(Currency.
CurrencyProperty instance of the valid property entry propReplaces currency data found in the properties file that java.util.currency.data designates. |
private static void | setMainTableEntry(char char1, char char2, int entry)
Sets the main table entry for the country whose country code consists of char1 and char2. |
public String | Returns: the ISO 4217 currency code of this currencyOverrides java. Returns the ISO 4217 currency code of this currency. |
private static void |
A_TO_Z | back to summary |
---|---|
private static final int A_TO_Z |
available | back to summary |
---|---|
private static HashSet<Currency> available |
COUNTRY_TYPE_MASK | back to summary |
---|---|
private static final int COUNTRY_TYPE_MASK |
COUNTRY_WITHOUT_CURRENCY_ENTRY | back to summary |
---|---|
private static final int COUNTRY_WITHOUT_CURRENCY_ENTRY |
currencyCode | back to summary |
---|---|
private final String currencyCode ISO 4217 currency code for this currency. |
dataVersion | back to summary |
---|---|
pack-priv static int dataVersion |
defaultFractionDigits | back to summary |
---|---|
private final transient int defaultFractionDigits Default fraction digits for this currency. Set from currency data tables. |
DISPLAYNAME | back to summary |
---|---|
private static final int DISPLAYNAME |
formatVersion | back to summary |
---|---|
pack-priv static int formatVersion |
instances | back to summary |
---|---|
private static ConcurrentMap<String, Currency> instances |
INVALID_COUNTRY_ENTRY | back to summary |
---|---|
private static final int INVALID_COUNTRY_ENTRY |
MAGIC_NUMBER | back to summary |
---|---|
private static final int MAGIC_NUMBER |
mainTable | back to summary |
---|---|
pack-priv static int[] mainTable |
NUMERIC_CODE_MASK | back to summary |
---|---|
private static final int NUMERIC_CODE_MASK |
NUMERIC_CODE_SHIFT | back to summary |
---|---|
private static final int NUMERIC_CODE_SHIFT |
numericCode | back to summary |
---|---|
private final transient int numericCode ISO 4217 numeric code for this currency. Set from currency data tables. |
otherCurrenciesList | back to summary |
---|---|
pack-priv static List<Currency. |
serialVersionUID | back to summary |
---|---|
private static final long serialVersionUID
|
SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_MASK | back to summary |
---|---|
private static final int SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_MASK |
SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_SHIFT | back to summary |
---|---|
private static final int SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_SHIFT |
SIMPLE_CASE_COUNTRY_FINAL_CHAR_MASK | back to summary |
---|---|
private static final int SIMPLE_CASE_COUNTRY_FINAL_CHAR_MASK |
SIMPLE_CASE_COUNTRY_MASK | back to summary |
---|---|
private static final int SIMPLE_CASE_COUNTRY_MASK |
SIMPLE_CASE_COUNTRY_MAX_DEFAULT_DIGITS | back to summary |
---|---|
private static final int SIMPLE_CASE_COUNTRY_MAX_DEFAULT_DIGITS |
SPECIAL_CASE_COUNTRY_INDEX_DELTA | back to summary |
---|---|
private static final int SPECIAL_CASE_COUNTRY_INDEX_DELTA |
SPECIAL_CASE_COUNTRY_INDEX_MASK | back to summary |
---|---|
private static final int SPECIAL_CASE_COUNTRY_INDEX_MASK |
SPECIAL_CASE_COUNTRY_MASK | back to summary |
---|---|
private static final int SPECIAL_CASE_COUNTRY_MASK |
specialCasesList | back to summary |
---|---|
pack-priv static List<Currency. |
SYMBOL | back to summary |
---|---|
private static final int SYMBOL Constants for retrieving localized names from the name providers. |
VALID_FORMAT_VERSION | back to summary |
---|---|
private static final int VALID_FORMAT_VERSION |
Currency | back to summary |
---|---|
private Currency(String currencyCode, int defaultFractionDigits, int numericCode) Constructs a |
getAvailableCurrencies | back to summary |
---|---|
public static Set Gets the set of available currencies. The returned set of currencies contains all of the available currencies, which may include currencies that represent obsolete ISO 4217 codes. The set can be modified without affecting the available currencies in the runtime. |
getCurrencyCode | back to summary |
---|---|
public String getCurrencyCode() Gets the ISO 4217 currency code of this currency.
|
getDefaultFractionDigits | back to summary |
---|---|
public int getDefaultFractionDigits() Gets the default number of fraction digits used with this currency. Note that the number of fraction digits is the same as ISO 4217's minor unit for the currency. For example, the default number of fraction digits for the Euro is 2, while for the Japanese Yen it's 0. In the case of pseudo-currencies, such as IMF Special Drawing Rights, -1 is returned.
|
getDisplayName | back to summary |
---|---|
public String getDisplayName() Gets the name that is suitable for displaying this currency for
the default
This is equivalent to calling
|
getDisplayName | back to summary |
---|---|
public String getDisplayName(Locale locale) Gets the name that is suitable for displaying this currency for the specified locale. If there is no suitable display name found for the specified locale, the ISO 4217 currency code is returned.
|
getInstance | back to summary |
---|---|
public static Currency getInstance(String currencyCode) Returns the
|
getInstance | back to summary |
---|---|
private static Currency getInstance(String currencyCode, int defaultFractionDigits, int numericCode) |
getInstance | back to summary |
---|---|
public static Currency getInstance(Locale locale) Returns the
If the specified
The method returns
|
getMainTableEntry | back to summary |
---|---|
private static int getMainTableEntry(char char1, char char2) Gets the main table entry for the country whose country code consists of char1 and char2. |
getNumericCode | back to summary |
---|---|
public int getNumericCode() Returns the ISO 4217 numeric code of this currency.
|
getNumericCodeAsString | back to summary |
---|---|
public String getNumericCodeAsString() Returns the 3 digit ISO 4217 numeric code of this currency as a
|
getSymbol | back to summary |
---|---|
public String getSymbol() Gets the symbol of this currency for the default
If the default
This is equivalent to calling
|
getSymbol | back to summary |
---|---|
public String getSymbol(Locale locale) Gets the symbol of this currency for the specified locale. For example, for the US Dollar, the symbol is "$" if the specified locale is the US, while for other locales it may be "US$". If no symbol can be determined, the ISO 4217 currency code is returned.
If the specified
|
getValidCurrencyData | back to summary |
---|---|
private static List Parse currency data found in the properties file (that java.util.currency.data designates) to a List of CurrencyProperty instances. Also, remove invalid entries and the multiple currency code inconsistencies.
|
initStatic | back to summary |
---|---|
private static void initStatic()
|
readIntArray | back to summary |
---|---|
private static int[] readIntArray(DataInputStream dis, int count) throws IOException |
readOtherCurrencies | back to summary |
---|---|
private static List |
readResolve | back to summary |
---|---|
private Object readResolve() Resolves instances being deserialized to a single instance per currency.
|
readSpecialCases | back to summary |
---|---|
private static List |
replaceCurrencyData | back to summary |
---|---|
private static void replaceCurrencyData(Currency. Replaces currency data found in the properties file that java.util.currency.data designates. This method is invoked for each valid currency entry.
|
setMainTableEntry | back to summary |
---|---|
private static void setMainTableEntry(char char1, char char2, int entry) Sets the main table entry for the country whose country code consists of char1 and char2. |
toString | back to summary |
---|---|
public String toString() Overrides java. Returns the ISO 4217 currency code of this currency. |
updateMainTableEntry | back to summary |
---|---|
private static void updateMainTableEntry(String code, int fraction, int numeric) |
Modifier and Type | Field and Description |
---|---|
private static final Currency. |
Access | Constructor and Description |
---|---|
private |
Modifier and Type | Method and Description |
---|---|
public String | getObject(CurrencyNameProvider
the provider currencyNameProvider, Locale the locale locale, String key string to localize, or null if the provider is not
a name provider key, Object... provider specific params params)Implements sun. Returns an object from the provider |
INSTANCE | back to summary |
---|---|
private static final Currency. |
CurrencyNameGetter | back to summary |
---|---|
private CurrencyNameGetter() |
getObject | back to summary |
---|---|
public String getObject(CurrencyNameProvider currencyNameProvider, Locale locale, String key, Object... params) Implements sun. Doc from sun. Returns an object from the provider |
Modifier and Type | Field and Description |
---|---|
private final String | |
private final String | |
private final String | |
private final int | |
private final int |
Access | Constructor and Description |
---|---|
private |
Modifier and Type | Method and Description |
---|---|
private static boolean | containsInconsistentInstances(List<Currency.
Checks if the given list contains multiple inconsistent currency instances |
private static Optional | Returns: Optional containing CurrencyProperty instance, If valid; empty otherwisecountry representing the currency data ctry, String currency data of the given curData, Pattern ctry regex pattern for the properties entry pattern)Check the valid currency data and create/return an Optional instance of CurrencyProperty |
private static void | |
private static boolean | |
private static Currency. |
country | back to summary |
---|---|
private final String country |
currencyCode | back to summary |
---|---|
private final String currencyCode |
date | back to summary |
---|---|
private final String date |
fraction | back to summary |
---|---|
private final int fraction |
numericCode | back to summary |
---|---|
private final int numericCode |
CurrencyProperty | back to summary |
---|---|
private CurrencyProperty(String country, String currencyCode, int fraction, int numericCode, String date) |
containsInconsistentInstances | back to summary |
---|---|
private static boolean containsInconsistentInstances(List<Currency. Checks if the given list contains multiple inconsistent currency instances |
getValidEntry | back to summary |
---|---|
private static Optional Check the valid currency data and create/return an Optional instance of CurrencyProperty |
info | back to summary |
---|---|
private static void info(String message, Throwable t) |
isPastCutoverDate | back to summary |
---|---|
private static boolean isPastCutoverDate(String s) throws ParseException |
parseProperty | back to summary |
---|---|
private static Currency. |
Modifier and Type | Field and Description |
---|---|
private final String | |
private final int | |
private final int |
Access | Constructor and Description |
---|---|
private |
Modifier and Type | Method and Description |
---|---|
private static Currency. |
currencyCode | back to summary |
---|---|
private final String currencyCode |
fraction | back to summary |
---|---|
private final int fraction |
numericCode | back to summary |
---|---|
private final int numericCode |
OtherCurrencyEntry | back to summary |
---|---|
private OtherCurrencyEntry(String currencyCode, int fraction, int numericCode) |
findEntry | back to summary |
---|---|
private static Currency. |
Modifier and Type | Field and Description |
---|---|
private final long | |
private final String | |
private final int | |
private final int | |
private final String | |
private final int | |
private final int |
Access | Constructor and Description |
---|---|
private | SpecialCaseEntry(long cutOverTime, String oldCurrency, String newCurrency, int oldCurrencyFraction, int newCurrencyFraction, int oldCurrencyNumericCode, int newCurrencyNumericCode)
|
private |
Modifier and Type | Method and Description |
---|---|
private static int | |
private static int[] | |
private static int | |
private static int |
cutOverTime | back to summary |
---|---|
private final long cutOverTime |
newCurrency | back to summary |
---|---|
private final String newCurrency |
newCurrencyFraction | back to summary |
---|---|
private final int newCurrencyFraction |
newCurrencyNumericCode | back to summary |
---|---|
private final int newCurrencyNumericCode |
oldCurrency | back to summary |
---|---|
private final String oldCurrency |
oldCurrencyFraction | back to summary |
---|---|
private final int oldCurrencyFraction |
oldCurrencyNumericCode | back to summary |
---|---|
private final int oldCurrencyNumericCode |
SpecialCaseEntry | back to summary |
---|---|
private SpecialCaseEntry(long cutOverTime, String oldCurrency, String newCurrency, int oldCurrencyFraction, int newCurrencyFraction, int oldCurrencyNumericCode, int newCurrencyNumericCode) |
SpecialCaseEntry | back to summary |
---|---|
private SpecialCaseEntry(String currencyCode, int fraction, int numericCode) |
currencyCodeIndex | back to summary |
---|---|
private static int currencyCodeIndex(String code) |
findEntry | back to summary |
---|---|
private static int[] findEntry(String code) |
indexOf | back to summary |
---|---|
private static int indexOf(String code, int fraction, int numeric) |
toIndex | back to summary |
---|---|
private static int toIndex(int tableEntry) |