Open In App

Locale toLanguageTag() Method in Java with Examples

The toLanguageTag() method of Locale class in Java is used to return a well-formed IETF BCP 47 language tag representing this locale object. Now there can be few complications when a language, country or variant does not satisfy the above-mentioned tag which is handled well by this method:

Syntax:



public String toLanguageTag()

Parameters: This method does not take any parameters.

Return Value: This method returns IETF BCP 47 language tag representation of this locale.



Below programs illustrate the working of toLanguageTag() method:
Program 1:




// Java code to illustrate
// toLanguageTag() method
  
import java.util.*;
  
public class Locale_Demo {
    public static void main(String[] args)
    {
  
        // Creating a new locale
        Locale first_locale
            = new Locale("Germany");
  
        // Displaying first locale
        System.out.println("Locale: "
                           + first_locale);
  
        // Displaying the LanguageTag
        System.out.println("The LanguageTag: "
                           + first_locale.toLanguageTag());
    }
}

Output:
Locale: germany
The LanguageTag: germany

Program 2:




// Java code to illustrate
// toLanguageTag() method
  
import java.util.*;
  
public class Locale_Demo {
    public static void main(String[] args)
    {
  
        // Creating a new locale
        Locale first_locale
            = new Locale("en", "USA");
  
        // Displaying first locale
        System.out.println("Locale: "
                           + first_locale);
  
        // Displaying the LanguageTag
        System.out.println("The LanguageTag: "
                           + first_locale.toLanguageTag());
    }
}

Output:
Locale: en_USA
The LanguageTag: en

Reference: https://docs.oracle.com/javase/7/docs/api/java/util/Locale.html#toLanguageTag()


Article Tags :