locale()方法是java.util.Formatter的内置方法,该方法返回语言环境。此区域设置由格式化程序构造设置。具有语言环境参数的该对象的format方法不会更改此值。
用法:
public Locale locale()
参数:该函数不接受任何参数。
返回值:如果未应用任何本地化,则该函数返回null,否则返回已初始化给格式化程序的语言环境。
异常注意:如果在调用函数之前关闭了格式化程序,则该函数将引发FormatterClosedException。
下面是上述函数的实现:
示例1:
// Java program to implement
// the above function
import java.util.Formatter;
import java.util.Locale;
public class Main {
public static void main(String[] args)
{
// Get the string Buffer
StringBuffer buffer
= new StringBuffer();
// Object creation
Formatter frmt
= new Formatter(buffer,
Locale.CANADA);
// Format a new string
String name = "My name is Gopal Dave";
frmt.format("What is your name? \n%s !",
name);
// Print the Formatted string
System.out.println(frmt);
// Prints the format that has been set
// Initially to the formatter
System.out.println("Locale: "
+ frmt.locale());
}
}
输出:
What is your name? My name is Gopal Dave ! Locale: en_CA
示例2:
// Java program to implement
// the above function
import java.util.Formatter;
import java.util.Locale;
public class Main {
public static void main(String[] args)
{
try {
// Get the string Buffer
StringBuffer buffer
= new StringBuffer();
// Object creation
Formatter frmt
= new Formatter(buffer,
Locale.CANADA);
// Format a new string
String name = "My name is Gopal Dave";
frmt.format("What is your name? \n%s !",
name);
// Print the Formatted string
System.out.println(frmt);
// Formatter closed
frmt.close();
// Prints the format that has been set
// Initially to the formatter
System.out.println("Locale: "
+ frmt.locale());
}
catch (Exception e) {
System.out.println("Exception is: "
+ e);
}
}
}
输出:
What is your name? My name is Gopal Dave ! Exception is: java.util.FormatterClosedException
参考: https://docs.oracle.com/javase/10/docs/api/java/util/Formatter.html#locale()
相关用法
- Java Locale getDisplayVariant(Locale inLoc)用法及代码示例
- Java Locale.Builder setLocale(Locale)用法及代码示例
- Java TimeZone getDisplayName(Locale locale)用法及代码示例
- Java Locale getDisplayCountry(Locale)用法及代码示例
- Java Locale getDisplayLanguage(Locale)用法及代码示例
- Java Locale getDisplayName(Locale)用法及代码示例
- Java Formatter out()用法及代码示例
- Java Formatter ioException()用法及代码示例
- Java Formatter close()用法及代码示例
- Java Formatter flush()用法及代码示例
- Java Locale hashCode()用法及代码示例
- Java Locale getDisplayCountry()用法及代码示例
- Java Locale getExtensionKeys()用法及代码示例
- Java Locale equals()用法及代码示例
注:本文由纯净天空筛选整理自gopaldave大神的英文原创作品 Formatter locale() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。