Java中的 Locale.Builder setUnicodeLocaleKeyword() 方法及示例
Java中Java .util.Locale.Builder类的setUnicodeLocaleKeyword()方法用来设置这个Locale.Builder为指定的unicodeLocaleKeyword键。这意味着此方法将设置 Locale.Builder 实例的当前 unicodeLocaleKeyword 以匹配提供的 unicodeLocaleKeyword 键并键入并返回它。如果指定的 unicodeLocaleKeyword 为 null 或为空,则删除此 LocaleBuilder 的 unicodeLocaleKeyword。
句法:
public Locale.Builder
setUnicodeLocaleKeyword(
String unicodeLocaleKeywordKey,
String unicodeLocaleKeywordType)
参数:此方法接受两个参数:
- unicodeLocaleKey :这是要设置为此 Locale.Builder 实例的 unicode Locale 的键字符串
- unicodeLocaleKeywordType :这是要设置为此 Locale.Builder 实例的 unicode Locale 类型 String
返回类型:此方法返回一个Locale.Builder 实例,并将此 Locale.Builder 的 unicodeLocaleKeyword 设置为指定的 unicodeLocaleKeyword。
异常:此方法抛出以下异常:
- IllformedLocaleException : 如果指定的 unicodeLocaleKeyword 有任何格式错误的字段
方案一:
// Java program to demonstrate
// the above method
import java.util.*;
import java.util.Locale.*;
public class LocaleBuilderDemo {
public static void main(String[] args)
{
// Creating a new Locale.Builder
Locale.Builder localeBuilder
= new Builder();
// Displaying Locale.Builder
System.out.println("LocaleBuilder: "
+ localeBuilder);
// setting the unicodeLocaleKeyword
// of Locale.Builder
String unicodeLocaleKeywordKey = "nu";
String unicodeLocaleKeywordType = "thai";
System.out.println("Setting the "
+ "unicodeLocaleKeyword: "
+ unicodeLocaleKeywordKey + "-"
+ unicodeLocaleKeywordType);
localeBuilder
= localeBuilder
.setUnicodeLocaleKeyword(
unicodeLocaleKeywordKey,
unicodeLocaleKeywordType);
// Displaying Locale.Builder
System.out.println("Updated LocaleBuilder: "
+ localeBuilder);
}
}
输出:
LocaleBuilder: java.util.Locale$Builder@232204a1
Setting the unicodeLocaleKeyword: nu-thai
Updated LocaleBuilder: java.util.Locale$Builder@232204a1
方案二:
// Java program to demonstrate
// the above method
import java.util.*;
import java.util.Locale.*;
public class LocaleBuilderDemo {
public static void main(String[] args)
{
// Creating a new Locale.Builder
Locale.Builder localeBuilder
= new Builder();
// Displaying Locale.Builder
System.out.println("LocaleBuilder: "
+ localeBuilder);
// setting the unicodeLocaleKeyword
// of Locale.Builder
String unicodeLocaleKeywordKey = "asf@";
String unicodeLocaleKeywordType = "afaf$";
System.out.println("Setting the "
+ "unicodeLocaleKeyword: "
+ unicodeLocaleKeywordKey + "-"
+ unicodeLocaleKeywordType);
try {
localeBuilder
= localeBuilder
.setUnicodeLocaleKeyword(
unicodeLocaleKeywordKey,
unicodeLocaleKeywordType);
// Displaying Locale.Builder
System.out.println("Updated LocaleBuilder: "
+ localeBuilder);
}
catch (Exception e) {
System.out.println(e);
}
}
}
输出:
LocaleBuilder: java.util.Locale$Builder@232204a1
Setting the unicodeLocaleKeyword: asf@-afaf$
java.util.IllformedLocaleException:
Ill-formed Unicode locale keyword key:
asf@ [at index 0]
参考: https://docs.oracle.com/javase/9/docs/api/ Java/util/Locale.Builder.html#setUnicodeLocaleKeyword-java.lang.String-java.lang.String-