Java Locale getDisplayName()

In this guide, you will learn about the Locale getDisplayName() method in Java programming and how to use it with an example.

1. Locale getDisplayName() Method Overview

Definition:

The getDisplayName() method of the Locale class in Java returns a name suitable for display to the user. This name is presented in a manner that is appropriate for the default locale or a specified locale.

Syntax:

1. public String getDisplayName() 
2. public String getDisplayName(Locale inLocale)

Parameters:

- inLocale: An optional parameter representing the locale in which to get the display name.

Key Points:

- If no locale is passed as an argument, the default system locale is used for the display name.

- This method is useful in applications where you need to present a human-readable name for a locale, allowing users to choose or recognize a locale setting.

2. Locale getDisplayName() Method Example

import java.util.Locale;

public class LocaleDisplayNameExample {
    public static void main(String[] args) {
        Locale frenchLocale = new Locale("fr", "FR");
        // Display name using the default system locale
        System.out.println("Default display name: " + frenchLocale.getDisplayName());

        // Display name using a specified locale (English in this case)
        System.out.println("English display name: " + frenchLocale.getDisplayName(Locale.ENGLISH));
    }
}

Output:

Default display name: French (France)
English display name: French (France)

Explanation:

In the example above, a Locale object representing the French locale for France is created. The getDisplayName() method is then used twice: once without any parameters (which uses the default system locale) and once with the English locale specified as the parameter. In both cases, since the English name for the French locale is "French (France)", the output is the same.

Comments