📌  相关文章
📜  Java中的 Locale.Builder setExtension() 方法及示例

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

Java中的 Locale.Builder setExtension() 方法及示例

Java中Java .util.Locale.Builder 类setExtension()方法用于将此Locale.Builder 设置为指定的扩展键。这意味着此方法将设置 Locale.Builder 实例的当前扩展名以匹配提供的扩展名键和值并返回。如果指定的扩展为 null 或为空,则移除此 LocaleBuilder 的扩展。根据LDML BCP 47 兼容扩展检查指定的扩展

句法:

public Locale.Builder setExtension(
                          char extensionKey, 
                          String extensionValue)

参数:此方法接受两个参数:

  • extensionKey :这是要设置为此 Locale>Builder 实例的字符值。
  • extensionValue :这是要设置为此 Locale>Builder 实例的字符串值。

返回值:该方法返回一个Locale.Builder实例,该实例带有这个Locale.Builder的扩展集到指定的扩展。

异常:此方法抛出以下异常:

  • IllformedLocaleException : 如果指定的扩展有任何格式错误的字段

方案一:

// 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 extension of Locale.Builder
        char extensionKey = 'u';
        String extensionValue = "ca-japanese";
        System.out.println("Setting the extension: "
                           + extensionKey + "/"
                           + extensionValue);
  
        localeBuilder
            = localeBuilder
                  .setExtension(extensionKey,
                                extensionValue);
  
        // Displaying Locale.Builder
        System.out.println("Updated LocaleBuilder: "
                           + localeBuilder);
    }
}
输出:
LocaleBuilder: java.util.Locale$Builder@232204a1
Setting the extension: u/ca-japanese
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 extension of Locale.Builder
        char extensionKey = '@';
        String extensionValue = "fsdf#";
        System.out.println("Setting the extension: "
                           + extensionKey + "/"
                           + extensionValue);
  
        try {
  
            localeBuilder
                = localeBuilder
                      .setExtension(extensionKey,
                                    extensionValue);
  
            // Displaying Locale.Builder
            System.out.println("Updated LocaleBuilder: "
                               + localeBuilder);
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}
输出:
LocaleBuilder: java.util.Locale$Builder@232204a1
Setting the extension: @/fsdf#
java.util.IllformedLocaleException:
 Ill-formed extension key:
 @ [at index 0]

参考: https://docs.oracle.com/javase/9/docs/api/ Java/util/Locale.Builder.html#setExtension-char-java.lang.String-