Related Articles

Related Articles

Collator getAvailableLocales() method in Java with Example
  • Last Updated : 03 Jan, 2020

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 initalizing new simple rule
            String simple = "< a< b< c< d";
  
            // Creating and initializing
            // new RuleBasedCollator Object
            RuleBasedCollator col
                = new RuleBasedCollator(simple);
  
            // getting all the avalable 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);
        }
    }
}

chevron_right


Output:

Equivalent Locales are 
ar_AE
ar_JO
ar_SY
hr_HR
fr_BE

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 avalable 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);
        }
    }
}

chevron_right


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.




My Personal Notes arrow_drop_up
Recommended Articles
Page :