Java Currency - getInstance() Method
The java.util.Currency.getInstance() method returns the Currency instance for the country of the given locale.
Syntax
public static Currency getInstance(Locale locale)
Parameters
locale |
Specify the locale for whose country a Currency instance is needed. |
Return Value
Returns the Currency instance for the country of the given locale, or null.
Exception
- Throws NullPointerException, if locale or its country code is null.
- Throws IllegalArgumentException, if the country of the given locale is not a supported ISO 3166 country code.
Example:
In the example below, the java.util.Currency.getInstance() method is used to get the currency instance for UK.
import java.util.*; public class MyClass { public static void main(String[] args) { //creating a currency for UK locale Locale locale = Locale.UK; Currency Curr = Currency.getInstance(locale); //getting and printing the ISO 4217 currency code String CurrCode = Curr.getCurrencyCode(); System.out.println("ISO 4217 currency code is: " + CurrCode); } }
The output of the above code will be:
ISO 4217 currency code is: GBP
❮ Java.util - Currency