Java.util.Currency.getInstance() Method


Advertisements


Description

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

Declaration

Following is the declaration for java.util.Currency.getInstance() method

public static Currency getInstance(Locale locale)

Parameters

locale − the locale for whose country a Currency instance is required

Return Value

This method returns the Currency instance for the country of the given locale, or null

Exception

  • NullPointerException − if country or locale code is null

  • IllegalArgumentException − if ISO 3166 does not support the given locale's country code.

Example

The following example shows the usage of java.util.Currency.getInstance() method.

Live Demo
package com.tutorialspoint;

import java.util.*;

public class CurrencyDemo {
   public static void main(String args[]) {

      // create a currency object with specified locale
      Locale locale = Locale.GERMANY;
      Currency curr = Currency.getInstance(locale);

      // print currency's code
      System.out.println("Locale's currency code:" + curr.getCurrencyCode());
   }
}

Let us compile and run the above program, this will produce the following result −

Locale's currency code:EUR

java_util_currency.htm

Advertisements