📌  相关文章
📜  Java中的 Collator getAvailableLocales() 方法与示例

📅  最后修改于: 2022-05-13 01:54:21.184000             🧑  作者: Mango

Java中的 Collator getAvailableLocales() 方法与示例

Java.text.Collator 类getAvailableLocales()方法用于在 Collator 对象初始化期间获取传入的 Locales 实例下的所有可用语言环境。
句法:

public static Locale[] getAvailableLocales()

参数:此方法不接受任何参数。
返回值:此方法返回此实例下的所有可用语言环境
下面是说明getAvailableLocales()方法的示例:
示例 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() method
            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);
        }
    }
}


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() method
            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);
        }
    }
}


输出:
Equivalent Locales are 
ar_AE
ar_JO
ar_SY
hr_HR
fr_BE

示例 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() method
            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);
        }
    }
}
输出:
Equivalent Locales are 
es_PA
es_VE
mt_MT
bg
zh_TW

参考: https://docs.oracle.com/javase/9/docs/api/ Java/text/Collator.html#getAvailableLocales–