Java Programming Tutorials

Java programming tutorials with many code examples!

java.util.Currency.getInstance(Locale)

java.util.Currency.getInstance(Locale)

The method java.util.Currency.getInstance(Locale) returns the Currency instance for the country of the given locale.

Method signature

The signature of the java.util.Currency.getInstance(Locale) method is as follows:

public static Currency getInstance(Locale locale)

Parameters

  • Locale: 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.

Exceptions

  • NullPointerException: if locale or its country code is null
  • IllegalArgumentException: if the country of the given locale is not a supported ISO 3166 country code

Example usage

In the following code we use java.util.Currency.getInstance(Locale):

package com.farenda.java.util;

import java.util.Currency;
import java.util.Locale;

public class CurrencyGetInstanceLocale {

    public static void main(String[] args) {
        Locale locale = Locale.CHINA;
        Currency currency = Currency.getInstance(locale);
        System.out.printf("Currency of %s: %s%n",
                locale, currency.getDisplayName());

        locale = new Locale("pl", "PL");
        currency = Currency.getInstance(locale);
        System.out.printf("Currency of %s: %s%n",
                locale, currency.getDisplayName());
    }
}

The above code produces the following output:

Currency of zh_CN: Chinese Yuan
Currency of pl_PL: Polish Zloty
Share with the World!