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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。