Java中的java.util.Locale.Builder类的setRegion(String)方法用于将此Locale.Builder设置为指定区域。这意味着此方法将设置Locale.Builder实例的当前区域以匹配提供的区域并返回它。如果指定的区域为null或为空,则将删除此LocaleBuilder的区域。格式正确的区域值包括ISO 3166标准定义的2个字母的区域代码或3位数字的UN M.49地区代码。
用法:
public Locale.Builder setRegion(String region)
参数:此方法接受区域作为参数,该参数是要设置为此Locale.Builder实例的String。
返回值:此方法返回一个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 region of Locale.Builder
String region = "IN";
System.out.println("Setting the region:"
+ region);
localeBuilder
= localeBuilder.setRegion(region);
// Displaying Locale.Builder
System.out.println("Updated LocaleBuilder:"
+ localeBuilder);
}
}
输出:
LocaleBuilder:java.util.Locale$Builder@232204a1 Setting the region:IN 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 region of Locale.Builder
String region = "asdasf@!vsd#";
System.out.println("Setting the region:"
+ region);
try {
localeBuilder
= localeBuilder.setRegion(region);
// Displaying Locale.Builder
System.out.println("Updated LocaleBuilder:"
+ localeBuilder);
}
catch (Exception e) {
System.out.println(e);
}
}
}
输出:
LocaleBuilder:java.util.Locale$Builder@232204a1 Setting the region:asdasf@!vsd# java.util.IllformedLocaleException: Ill-formed region: asdasf@!vsd# [at index 0]
参考: https://docs.oracle.com/javase/9/docs/api/java/util/Locale.Builder.html#setRegion-java.lang.String-
相关用法
- Java Java.util.Collections.rotate()用法及代码示例
- Java Java lang.Long.highestOneBit()用法及代码示例
- Java Java lang.Long.byteValue()用法及代码示例
- Java Java.util.Collections.disjoint()用法及代码示例
- Java Java lang.Long.numberOfLeadingZeros()用法及代码示例
- Java Java lang.Long.reverse()用法及代码示例
- Java Java lang.Long.lowestOneBit()用法及代码示例
- Java Java lang.Long.numberOfTrailingZeros()用法及代码示例
- Java Java lang.Long.builtcount()用法及代码示例
- Java Clock withZone()用法及代码示例
- Java Clock tickMinutes()用法及代码示例
- Java Map put()用法及代码示例
- Java Set contains()用法及代码示例
注:本文由纯净天空筛选整理自Code_r大神的英文原创作品 Locale.Builder setRegion() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。