MessageFormat setLocale() method in Java with Example
Last Updated :
20 Jan, 2023
The setLocale() method of java.text.MessageFormat class is used to set the new locale in this message format object.
Syntax:
public void setLocale(Locale locale)
Parameter: This method takes new locale object as a parameter.
Return Value: This method has nothing to return.
Below are the examples to illustrate the setLocale() method:
Example 1:
Java
import java.text.*;
import java.util.*;
import java.io.*;
public class GFG {
public static void main(String[] argv)
{
MessageFormat mf
= new MessageFormat("{ 0 , number, #}");
mf.setLocale( new Locale("en-US"));
Locale locale = mf.getLocale();
System.out.println("Locale is : " + locale);
}
}
|
Output:
Locale is : en-us
Example 2:
Java
import java.text.*;
import java.util.*;
import java.io.*;
public class GFG {
public static void main(String[] argv)
{
MessageFormat mf
= new MessageFormat("{ 0 , number, #}, { 2 , date, #.#}, { 4 , time}");
mf.setLocale( new Locale("zh-Hant-TW"));
Locale locale = mf.getLocale();
System.out.println("Locale is : " + locale);
}
}
|
Output:
Locale is : zh-hant-tw
Reference: https://docs.oracle.com/javase/9/docs/api/java/text/MessageFormat.html#setLocale-java.util.Locale-
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...