當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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:這是要為Unicode語言環境設置為此Locale.Builder實例的鍵String。
  • unicodeLocaleKeywordType:這是將設置為Unicode語言環境的此Locale.Builder實例的String類型。

返回類型:此方法返回一個Locale.Builder實例,並將此Locale.Builder的unicodeLocaleKeyword集設置為指定的unicodeLocaleKeyword。

異常:此方法引發以下異常:

  • IllformedLocaleException:如果指定的unicodeLocaleKeyword具有任何格式錯誤的字段

程序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 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

程序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 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-



相關用法


注:本文由純淨天空篩選整理自Code_r大神的英文原創作品 Locale.Builder setUnicodeLocaleKeyword() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。