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

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

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

Java中Java .util.Locale.Builder 类build()方法用于从指定给此 Locale.Builder 实例的值构建一个 Locale 。此方法在构建后返回一个 Locale 实例。

句法:

public Locale build()

参数:此方法不接受任何参数。

返回类型:此方法返回一个Locale实例,其值设置为此 Locale.Builder。

异常:此方法不抛出任何异常。

方案一:

// 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();
  
        // setting the locale of Locale.Builder
        Locale locale = Locale.FRANCE;
        System.out.println("Setting the Locale: "
                           + locale);
        localeBuilder.setLocale(locale);
  
        // Displaying Locale.Builder
        System.out.println("LocaleBuilder: "
                           + localeBuilder);
  
        // Building the Locale from Locale.Builder
        System.out.println("Building the Locale.");
  
        Locale builtLocale = localeBuilder.build();
  
        // Displaying Locale.Builder
        System.out.println("Built Locale: "
                           + builtLocale);
    }
}
输出:
Setting the Locale: fr_FR
LocaleBuilder: java.util.Locale$Builder@232204a1
Building the Locale.
Built Locale: fr_FR

方案二:

// 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();
  
        // setting the locale of Locale.Builder
        Locale locale = Locale.ENGLISH;
        System.out.println("Setting the Locale: "
                           + locale);
        localeBuilder.setLocale(locale);
  
        // Displaying Locale.Builder
        System.out.println("LocaleBuilder: "
                           + localeBuilder);
  
        // Building the Locale from Locale.Builder
        System.out.println("Building the Locale.");
  
        Locale builtLocale = localeBuilder.build();
  
        // Displaying Locale.Builder
        System.out.println("Built Locale: "
                           + builtLocale);
    }
}
输出:
Setting the Locale: en
LocaleBuilder: java.util.Locale$Builder@232204a1
Building the Locale.
Built Locale: en

参考: https://docs.oracle.com/javase/9/docs/api/ Java/util/Locale.Builder.html#build–