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


Java Locale.Builder setLocale(Locale)用法及代碼示例


Java中的java.util.Locale.Builder類的setLocale(Locale)方法用於將此Locale.Builder重置為指定的Locale。這意味著此方法將重置Locale.Builder實例的當前狀態以匹配提供的Locale並返回它。

用法:

public Locale.Builder setLocale(Locale locale)

參數:此方法接受語言環境作為參數,該參數是要設置為此Locale.Builder實例的語言環境。


返回值:此方法返回一個Locale.Builder實例,該實例是將此Locale.Builder設置為指定Locale的狀態。

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

  • IllformedLocaleException:如果指定的語言環境具有任何格式錯誤的字段
  • NullPointerException:如果指定的語言環境為null

程序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 locale of Locale.Builder 
        Locale locale = Locale.FRANCE; 
        System.out.println("Setting the Locale:"
                           + locale); 
  
        localeBuilder 
            = localeBuilder.setLocale(locale); 
  
        // Displaying Locale.Builder 
        System.out.println("Updated LocaleBuilder:"
                           + localeBuilder); 
    } 
}
輸出:
LocaleBuilder:java.util.Locale$Builder@232204a1
Setting the Locale:fr_FR
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 locale of Locale.Builder 
        Locale locale = Locale.ENGLISH; 
        System.out.println("Setting the Locale:"
                           + locale); 
  
        localeBuilder 
            = localeBuilder.setLocale(locale); 
  
        // Displaying Locale.Builder 
        System.out.println("Updated LocaleBuilder:"
                           + localeBuilder); 
    } 
}
輸出:
LocaleBuilder:java.util.Locale$Builder@232204a1
Setting the Locale:en
Updated LocaleBuilder:java.util.Locale$Builder@232204a1

參考: https://docs.oracle.com/javase/9/docs/api/java/util/Locale.Builder.html#setLocale-java.util.Locale-



相關用法


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