The java.util.Currency class has been available since JDK 1.4, but JDK 7 has provided some useful new methods at the class and instance level. I look at these new methods in this post.
The Currency
class represents worldwide currencies as "identified by their ISO 4217 currency codes" available at the International Organization for Standardization site. Although one can override the represented currencies available in an instance of the JVM by placing an appropriate properties file in <JAVA_HOME>/lib/currency.properties
, most of us are likely to use the currencies provided out-of-the-box with the JVM. It is easy to find out what these available currencies are with the new-to-JDK 7 class-level (static) function getAvailableCurrencies().
I like it when the JDK provides access to runtime JVM-specific details. The addition of Currency.getAvailableCurrencies()
brings this support to Currency
similar to what is available for locales [Locale.getAvailableLocales()], time zones [TimeZone.getAvailableIDs()], characters sets [CharSet.availableCharsets()], and available JVM scripting engines [ScriptEngineManager.getEngineFactories()]. It is easy to invoke the static function with a single line Groovy script called directly with the -e option:
There are three new instance-level methods added to Currency
in JDK 7. Two of them [getDisplayName() and getDisplayName(Locale)] provide a String-based name of the currency instance (one for default locale and one for a provided locale). The third new method [getNumericCode()] provides an int
representing the numeric code for that currency instance.
The next code listing is of a simple Java application that runs through all available currencies using the method added to JDK 7 and then prints the numeric code for each currency along with the two versions of the currency display name as provided by new JDK 7 methods.
Example Demonstrating JDK 7 Methods on Currencypackage dustin.examples; import static java.lang.System.out; import java.util.Currency; import java.util.Locale; import java.util.Set; /** * Demonstrate methods added to java.util.Currency in JDK 7. * * @author Dustin */ public class Main { /** * Main executable function for demonstrating Currency methods added in JDK 7. * * @param arguments The command line arguments; none expected. */ public static void main(String[] arguments) { final Set<Currency> currencies = Currency.getAvailableCurrencies(); for (final Currency currency : currencies) { out.println( "Currency: " + currency.getDisplayName() + "/" + currency.getDisplayName(Locale.ITALIAN) + "/" + currency.getNumericCode()); } } }
When the above is executed on the JVM I'm using, the output appears as follows:
Currency: Venezuelan Bolívar/Bolívar venezuelano forte/937 Currency: Colombian Peso/Peso Colombiano/170 Currency: French Franc/Franco Francese/250 Currency: Kyrgystani Som/Som Kirghiso/417 Currency: Euro/Euro/978 Currency: Maltese Lira/Lira Maltese/470 Currency: Georgian Lari/Lari Georgiano/981 Currency: Afghan Afghani (1927-2002)/Afgani (1927-2002)/4 Currency: US Dollar (Next day)/Dollaro Statunitense (Next day)/997 Currency: Mozambican Metical/Metical del Mozambico/943 Currency: Dutch Guilder/Fiorino Olandese/528 Currency: Panamanian Balboa/Balboa di Panama/590 Currency: Qatari Rial/Rial del Qatar/634 Currency: Bahamian Dollar/Dollaro delle Bahamas/44 Currency: Djiboutian Franc/Franco Gibutiano/262 Currency: Serbian Dinar (2002-2006)/Serbian Dinar (2002-2006)/891 Currency: New Zealand Dollar/Dollaro Neozelandese/554 Currency: Syrian Pound/Sterlina Siriana/760 Currency: Malagasy Franc/Franco Malgascio/450 Currency: Venezuelan Bolívar (1871-2008)/Bolivar Venezuelano/862 Currency: Saudi Riyal/Ryal Saudita/682 Currency: Rwandan Franc/Franco Ruandese/646 Currency: Yemeni Rial/Rial dello Yemen/886 Currency: Barbadian Dollar/Dollaro di Barbados/52 Currency: Sierra Leonean Leone/Leone della Sierra Leone/694 Currency: Sri Lankan Rupee/Rupia di Sri Lanka/144 Currency: Chilean Peso/Peso Cileno/152 Currency: Bermudan Dollar/Dollaro delle Bermuda/60 Currency: Omani Rial/Rial Omanita/512 Currency: Myanma Kyat/Kyat di Myanmar/104 Currency: Gambian Dalasi/Dalasi del Gambia/270 Currency: Guatemalan Quetzal/Quetzal Guatemalteco/320 Currency: CFP Franc/Franco CFP/953 Currency: Turkmenistani Manat (1993-2009)/Manat Turkmeno/795 Currency: Romanian Leu/Romanian Leu/946 Currency: Singapore Dollar/Dollaro di Singapore/702 Currency: Jamaican Dollar/Dollaro Giamaicano/388 Currency: Bolivian Boliviano/Boliviano/68 Currency: Eritrean Nakfa/Nakfa Eritreo/232 Currency: Turkmenistani Manat/Turkmenistani Manat/934 Currency: Mongolian Tugrik/Tugrik Mongolo/496 Currency: Indonesian Rupiah/Rupia Indonesiana/360 Currency: ZWN/ZWN/942 Currency: Luxembourgian Franc/Franco del Lussemburgo/442 Currency: Russian Ruble (1991-1998)/Rublo della CSI/810 Currency: São Tomé and Príncipe Dobra/Dobra di Sao Tomé e Principe/678 Currency: Ukrainian Hryvnia/Hrivna Ucraina/980 Currency: Argentine Peso/Peso Argentino/32 Currency: Bahraini Dinar/Dinaro del Bahraini/48 Currency: Belize Dollar/Dollaro Belize/84 Currency: Russian Ruble/Rublo Russo/643 Currency: Laotian Kip/Kip Laotiano/418 Currency: Maldivian Rufiyaa/Rufiyaa delle Maldive/462 Currency: Libyan Dinar/Dinaro Libico/434 Currency: Sudanese Dinar (1992-2007)/Dinaro Sudanese/736 Currency: Danish Krone/Corona Danese/208 Currency: Zimbabwean Dollar (2008)/Zimbabwean Dollar (2008)/935 Currency: Spanish Peseta/Peseta Spagnola/724 Currency: Macanese Pataca/Pataca di Macao/446 Currency: Surinamese Guilder/Fiorino del Suriname/740 Currency: Samoan Tala/Tala della Samoa Occidentale/882 Currency: Swazi Lilangeni/Lilangeni dello Swaziland/748 Currency: Tanzanian Shilling/Scellino della Tanzania/834 Currency: Portuguese Escudo/Escudo Portoghese/620 Currency: XUA/XUA/965 Currency: Armenian Dram/Dram Armeno/51 Currency: Greek Drachma/Dracma Greca/300 Currency: Guinean Franc/Franco della Guinea/324 Currency: Nicaraguan Córdoba/Córdoba oro nicaraguense/558 Currency: Gibraltar Pound/Sterlina di Gibilterra/292 Currency: Bolivian Mvdol/Mvdol Boliviano/984 Currency: Bulgarian Lev/Nuovo Lev Bulgaro/975 Currency: Belgian Franc/Franco Belga/56 Currency: Kazakhstani Tenge/Tenge Kazaco/398 Currency: Solomon Islands Dollar/Dollaro delle Isole Solomon/90 Currency: Costa Rican Colón/Colón Costaricano/188 Currency: Swiss Franc/Franco Svizzero/756 Currency: Turkish Lira/Turkish Lira/949 Currency: Australian Dollar/Dollaro Australiano/36 Currency: CFA Franc BCEAO/Franco CFA BCEAO/952 Currency: Uzbekistan Som/Sum dell’Uzbekistan/860 Currency: European Unit of Account (XBD)/Unità di acconto europea (XBD)/958 Currency: Chinese Yuan/Renmimbi Cinese/156 Currency: Malawian Kwacha/Kwacha Malawiano/454 Currency: Bhutanese Ngultrum/Ngultrum Butanese/64 Currency: Zimbabwean Dollar (2009)/Zimbabwean Dollar (2009)/932 Currency: Special Drawing Rights/Diritti Speciali di Incasso/960 Currency: Italian Lira/Lira Italiana/380 Currency: Moroccan Dirham/Dirham Marocchino/504 Currency: Chilean Unit of Account (UF)/Unidades de Fomento Chilene/990 Currency: Lesotho Loti/Loti del Lesotho/426 Currency: Nigerian Naira/Naira Nigeriana/566 Currency: Guinea-Bissau Peso/Peso della Guinea-Bissau/624 Currency: Sudanese Pound/Sterlina sudanese/938 Currency: Lebanese Pound/Sterlina Libanese/422 Currency: Mauritian Rupee/Rupia Mauriziana/480 Currency: Testing Currency Code/Testing Currency Code/963 Currency: Latvian Lats/Lat Lettone/428 Currency: Japanese Yen/Yen Giapponese/392 Currency: Turkish Lira (1922-2005)/Lira Turca/792 Currency: Norwegian Krone/Corona Norvegese/578 Currency: Indian Rupee/Rupia Indiana/356 Currency: Hungarian Forint/Fiorino Ungherese/348 Currency: Tunisian Dinar/Dinaro Tunisino/788 Currency: Congolese Franc/Franco Congolese/976 Currency: Andorran Peseta/Peseta Andorrana/20 Currency: Palladium/Palladio/964 Currency: Liberian Dollar/Dollaro Liberiano/430 Currency: Netherlands Antillean Guilder/Fiorino delle Antille Olandesi/532 Currency: US Dollar (Same day)/Dollaro Statunitense (Same day)/998 Currency: Somali Shilling/Scellino Somalo/706 Currency: Mexican Peso/Peso Messicano/484 Currency: Zimbabwean Dollar (1980-2008)/Dollaro dello Zimbabwe/716 Currency: British Pound Sterling/Sterlina Inglese/826 Currency: Albanian Lek/Lek Albanese/8 Currency: Jordanian Dinar/Dinaro Giordano/400 Currency: East Caribbean Dollar/Dollaro dei Caraibi Orientali/951 Currency: Cuban Convertible Peso/Cuban Convertible Peso/931 Currency: Gold/Oro/959 Currency: Czech Republic Koruna/Corona Ceca/203 Currency: US Dollar/Dollaro Statunitense/840 Currency: Cuban Peso/Peso Cubano/192 Currency: Comorian Franc/Franco Comoriano/174 Currency: Pakistani Rupee/Rupia del Pakistan/586 Currency: Fijian Dollar/Dollaro delle Figi/242 Currency: French UIC-Franc/Franco UIC Francese/0 Currency: Vietnamese Dong/Dong Vietnamita/704 Currency: Honduran Lempira/Lempira Hoduregno/340 Currency: Brunei Dollar/Dollaro del Brunei/96 Currency: Finnish Markka/Markka Finlandese/246 Currency: European Monetary Unit/Unità monetaria europea/956 Currency: United Arab Emirates Dirham/Dirham degli Emirati Arabi Uniti/784 Currency: Algerian Dinar/Dinaro Algerino/12 Currency: South African Rand/Rand Sudafricano/710 Currency: Burundian Franc/Franco del Burundi/108 Currency: Bangladeshi Taka/Taka Bangladese/50 Currency: Paraguayan Guarani/Guarani del Paraguay/600 Currency: XSU/XSU/994 Currency: Peruvian Nuevo Sol/Sol Nuevo Peruviano/604 Currency: Ghanaian Cedi (1979-2007)/Cedi del Ghana/288 Currency: North Korean Won/Won Nordcoreano/408 Currency: Slovenian Tolar/Tallero Sloveno/705 Currency: Philippine Peso/Peso delle Filippine/608 Currency: Mauritanian Ouguiya/Ouguiya della Mauritania/478 Currency: Unknown Currency/Nessuna valuta/999 Currency: Azerbaijani Manat (1993-2006)/Manat Azero (1993-2006)/31 Currency: Cayman Islands Dollar/Dollaro delle Isole Cayman/136 Currency: Croatian Kuna/Kuna Croata/191 Currency: Botswanan Pula/Pula del Botswana/72 Currency: Saint Helena Pound/Sterlina di Sant’Elena/654 Currency: AYM/AYM/945 Currency: Ugandan Shilling/Scellino Ugandese/800 Currency: Austrian Schilling/Scellino Austriaco/40 Currency: Malaysian Ringgit/Ringgit della Malesia/458 Currency: French Gold Franc/Franco Oro Francese/0 Currency: Irish Pound/Sterlina irlandese/372 Currency: Vanuatu Vatu/Vatu di Vanuatu/548 Currency: Serbian Dinar/Dinaro serbo/941 Currency: Iranian Rial/Rial Iraniano/364 Currency: Timorese Escudo/Escudo di Timor/626 Currency: Mexican Investment Unit/Unidad de Inversion (UDI) Messicana/979 Currency: Iraqi Dinar/Dinaro Iracheno/368 Currency: Romanian Leu (1952-2006)/Leu della Romania/946 Currency: Moldovan Leu/Leu Moldavo/498 Currency: Slovak Koruna/Corona Slovacca/703 Currency: Israeli New Sheqel/Nuovo sheqel israeliano/376 Currency: Polish Zloty/Zloty Polacco/985 Currency: Seychellois Rupee/Rupia delle Seychelles/690 Currency: European Unit of Account (XBC)/Unità di acconto europea (XBC)/957 Currency: Yugoslavian New Dinar (1994-2002)/Dinaro Noviy Yugoslavo/891 Currency: Macedonian Denar/Dinaro Macedone/807 Currency: Belarusian Ruble/Rublo Bielorussia/974 Currency: Egyptian Pound/Sterlina Egiziana/818 Currency: Hong Kong Dollar/Dollaro di Hong Kong/344 Currency: Guyanaese Dollar/Dollaro della Guyana/328 Currency: Bosnia-Herzegovina Convertible Mark/Marco Conv. Bosnia-Erzegovina/977 Currency: Thai Baht/Baht Tailandese/764 Currency: Kuwaiti Dinar/Dinaro Kuwaitiano/414 Currency: Kenyan Shilling/Scellino Keniota/404 Currency: Icelandic Króna/Corona Islandese/352 Currency: Falkland Islands Pound/Sterlina delle Falkland/238 Currency: Angolan Kwanza/Kwanza Angolano/973 Currency: Bulgarian Hard Lev/Lev Bulgaro/100 Currency: Malagasy Ariary/Ariary Malgascio/969 Currency: Surinamese Dollar/Dollaro surinamese/968 Currency: Dominican Peso/Peso Dominicano/214 Currency: Tongan Pa?anga/Pa?anga di Tonga/776 Currency: Papua New Guinean Kina/Kina della Papua Nuova Guinea/598 Currency: Cape Verdean Escudo/Escudo del Capo Verde/132 Currency: Namibian Dollar/Dollaro Namibiano/516 Currency: Uruguayan Peso/Peso Uruguayo uruguaiano/858 Currency: Ethiopian Birr/Birr Etiopico/230 Currency: Trinidad and Tobago Dollar/Dollaro di Trinidad e Tobago/780 Currency: Salvadoran Colón/Colón Salvadoregno/222 Currency: Brazilian Real/Real Brasiliano/986 Currency: Silver/Argento/961 Currency: Tajikistani Somoni/Somoni del Tajikistan/972 Currency: European Composite Unit/Unità composita europea/955 Currency: Lithuanian Litas/Lita Lituana/440 Currency: Estonian Kroon/Corona dell’Estonia/233 Currency: Platinum/Platinum/962 Currency: Haitian Gourde/Gourde Haitiano/332 Currency: Belarusian New Ruble (1994-1999)/Nuovo Rublo Bielorussia (1994-1999)/112 Currency: South Korean Won/Won Sudcoreano/410 Currency: Azerbaijani Manat/Azerbaijani Manat/944 Currency: Nepalese Rupee/Rupia Nepalese/524 Currency: Cambodian Riel/Riel Cambogiano/116 Currency: Zambian Kwacha/Kwacha dello Zambia/894 Currency: New Taiwan Dollar/Nuovo dollaro taiwanese/901 Currency: CFA Franc BEAC/Franco CFA BEAC/950 Currency: Cypriot Pound/Sterlina Cipriota/196 Currency: Afghan Afghani/Afgani/971 Currency: Aruban Florin/Fiorino di Aruba/533 Currency: Canadian Dollar/Dollaro Canadese/124 Currency: Mozambican Metical (1980-2006)/Mozambican Metical (1980-2006)/508 Currency: Ghanaian Cedi/Cedi ghanese/936 Currency: German Mark/Marco Tedesco/276 Currency: Swedish Krona/Corona Svedese/752
This blog post has briefly looked at the new class-level method and three new instance-level methods added to the Currency
class.
No comments:
Post a Comment