Java中的 MessageFormat getLocale() 方法与示例
Java.text.MessageFormat类的getLocale()方法用于获取此消息格式对象中使用的语言环境。
句法:
public Locale getLocale()
参数:此方法不接受任何参数作为参数。
返回值:此方法返回此消息格式对象中使用的语言环境。
以下是说明getLocale()方法的示例:
示例 1:
Java
// Java program to demonstrate
// getLocale() method
import java.text.*;
import java.util.*;
import java.io.*;
public class GFG {
public static void main(String[] argv)
{
// creating and initializing MessageFormat
MessageFormat mf
= new MessageFormat("{0, number, #}, {2, date, #.#}, {4, time}");
// getting Locale
// used in MessageFormat Object
// using getLocale() method
Locale locale = mf.getLocale();
// display the result
System.out.println("Locale is : " + locale);
}
}
Java
// Java program to demonstrate
// getLocale() method
import java.text.*;
import java.util.*;
import java.io.*;
public class GFG {
public static void main(String[] argv)
{
// creating and initializing MessageFormat
MessageFormat mf
= new MessageFormat("{0, number, #}, {2, date, #.#}, {4, time}");
// setting new Locale for message format object
mf.setLocale(new Locale("zh-Hant-TW"));
// getting Locale
// used in MessageFormat Object
// using getLocale() method
Locale locale = mf.getLocale();
// display the result
System.out.println("Locale is : " + locale);
}
}
输出:
Locale is : en_US
示例 2:
Java
// Java program to demonstrate
// getLocale() method
import java.text.*;
import java.util.*;
import java.io.*;
public class GFG {
public static void main(String[] argv)
{
// creating and initializing MessageFormat
MessageFormat mf
= new MessageFormat("{0, number, #}, {2, date, #.#}, {4, time}");
// setting new Locale for message format object
mf.setLocale(new Locale("zh-Hant-TW"));
// getting Locale
// used in MessageFormat Object
// using getLocale() method
Locale locale = mf.getLocale();
// display the result
System.out.println("Locale is : " + locale);
}
}
输出:
Locale is : zh-hant-tw
参考: https://docs.oracle.com/javase/9/docs/api/ Java/text/MessageFormat.html#getLocale–