Saturday, February 7, 2026
HomeLanguagesJavaMessageFormat setLocale() method in Java with Example

MessageFormat setLocale() method in Java with Example

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




// Java program to demonstrate
// setLocale() 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, #}");
 
        // setting new Locale for
        // message format object
        // using setLocale() method
        mf.setLocale(new Locale("en-US"));
 
        // getting Locale
        // used in MessageFormat Object
        Locale locale = mf.getLocale();
 
        // display the result
        System.out.println("Locale is : " + locale);
    }
}


Output:

Locale is : en-us

Example 2: 

Java




// Java program to demonstrate
// setLocale() 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
        // using setLocale() method
        mf.setLocale(new Locale("zh-Hant-TW"));
 
        // getting Locale
        // used in MessageFormat Object
        Locale locale = mf.getLocale();
 
        // display the result
        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-

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32492 POSTS0 COMMENTS
Milvus
126 POSTS0 COMMENTS
Nango Kala
6863 POSTS0 COMMENTS
Nicole Veronica
11987 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12079 POSTS0 COMMENTS
Shaida Kate Naidoo
6996 POSTS0 COMMENTS
Ted Musemwa
7239 POSTS0 COMMENTS
Thapelo Manthata
6947 POSTS0 COMMENTS
Umr Jansen
6934 POSTS0 COMMENTS