Java Utility Library

Java Locale - toLanguageTag() Method



The java.util.Locale.toLanguageTag() method returns a well-formed IETF BCP 47 language tag representing this locale.

Syntax

public String toLanguageTag()

Parameters

No parameter is required.

Return Value

Returns a BCP47 language tag representing the locale.

Exception

NA.

Example:

In the example below, the java.util.Locale.toLanguageTag() method is used to get a BCP47 language tag of the given locale.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a locale
    Locale loc = new Locale("th", "TH", "TH");

    //printing the locale
    System.out.println("loc is: " + loc);

    //printing the BCP47 language tag of the locale
    System.out.println("BCP47 language tag is: " + loc.toLanguageTag());
  }
}

The output of the above code will be:

loc is: th_TH_TH_#u-nu-thai
BCP47 language tag is: th-TH-u-nu-thai-x-lvariant-TH

❮ Java.util - Locale