Skip to content
Related Articles

Related Articles

Improve Article

Collator getAvailableLocales() method in Java with Example

  • Last Updated : 20 Jul, 2021
Geek Week

The getAvailableLocales() method of java.text.Collator class is used to get the all available locales which come under the passed Locales instance during the initializing of Collator object.
Syntax: 
 

public static Locale[] getAvailableLocales()

Parameter: This method does not accept any parameter.
Return Value: This method returns all the available locales which come under this instance.
Below are the examples to illustrate the getAvailableLocales() method:
Example 1: 
 

Java




// Java program to demonstrate
// getAvailableLocales() method
 
import java.text.*;
import java.util.*;
import java.io.*;
 
public class GFG {
    public static void main(String[] argv)
    {
        try {
 
            // Creating and initializing new simple rule
            String simple = "< a< b< c< d";
 
            // Creating and initializing
            // new RuleBasedCollator Object
            RuleBasedCollator col
                = new RuleBasedCollator(simple);
 
            // getting all the available locale
            // using getAvailableLocales() mehtod
            Locale[] locale = col.getAvailableLocales();
 
            // display result
            System.out.println("Equivalent Locales are ");
            for (int i = 1; i <= 5; i++)
                System.out.println(locale[i]);
        }
 
        catch (ClassCastException e) {
 
            System.out.println("Exception thrown : " + e);
        }
        catch (ParseException e) {
 
            System.out.println("Exception thrown : " + e);
        }
    }
}
Output: 
Equivalent Locales are 
ar_AE
ar_JO
ar_SY
hr_HR
fr_BE

 

Example 2: 
 

Java




// Java program to demonstrate
// getAvailableLocales() method
 
import java.text.*;
import java.util.*;
import java.io.*;
 
public class GFG {
    public static void main(String[] argv)
    {
        try {
 
            // Creating and initializing Collator Object
            Collator col = Collator.getInstance(Locale.UK);
 
            // getting all the available locale
            // using getAvailableLocales() mehtod
            Locale[] locale = col.getAvailableLocales();
 
            // display result
            System.out.println("Equivalent Locales are ");
            for (int i = 6; i <= 10; i++)
                System.out.println(locale[i]);
        }
 
        catch (ClassCastException e) {
 
            System.out.println("Exception thrown : " + e);
        }
    }
}
Output: 
Equivalent Locales are 
es_PA
es_VE
mt_MT
bg
zh_TW

 

Reference: https://docs.oracle.com/javase/9/docs/api/java/text/Collator.html#getAvailableLocales–
 

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :