当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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:将String值设置为此Locale> Builder实例。

返回值:此方法返回一个Locale.Builder实例,并将此Locale.Builder的扩展名设置为指定的扩展名。

异常:此方法引发以下异常:

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

程序1:

// 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

程序2:

// 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-



相关用法


注:本文由纯净天空筛选整理自Code_r大神的英文原创作品 Locale.Builder setExtension() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。