Java Locale forLanguageTag() Method



Description

The Java Locale forLanguageTag​(String languageTag) method returns a locale for the specified IETF BCP 47 language tag string.

Declaration

Following is the declaration for java.util.Locale.forLanguageTag() method

public static Locale forLanguageTag​(String languageTag)

Parameters

languageTag − the language tag.

tags − language tags.

Return Value

This method returns the locale that best represents the language tag.

Exception

NullPointerException − if languageTag is null

Creating a Locale for English Language Example

The following example shows the usage of Java Locale forLanguageTag(String) method. We're creating a locale of English using a string using forLanguageTag(). Then the locale is printed.

package com.tutorialspoint;

import java.util.Locale;

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

      Locale locale = Locale.forLanguageTag("en");

      System.out.println(locale);
   }
}

Output

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

en

Creating a Locale for French Language Example

The following example shows the usage of Java Locale forLanguageTag(String) method. We're creating a locale of French using a string using forLanguageTag(). Then the locale is printed.

package com.tutorialspoint;

import java.util.Locale;

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

      Locale locale = Locale.forLanguageTag("fr");

      System.out.println(locale);
   }
}

Output

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

fr

Creating a Locale for Japanese Language Example

The following example shows the usage of Java Locale forLanguageTag(String) method. We're creating a locale of Japanese using a string using forLanguageTag(). Then the locale is printed.

package com.tutorialspoint;

import java.util.Locale;

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

      Locale locale = Locale.forLanguageTag("jp");

      System.out.println(locale);
   }
}

Output

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

jp
java_util_locale.htm
Advertisements