Friday, September 5, 2025
HomeLanguagesJavaMessageFormat getLocale() method in Java with Example

MessageFormat getLocale() method in Java with Example

The getLocale() method of java.text.MessageFormat class is used to get the locale used in this message format object.
Syntax: 
 

public Locale getLocale()

Parameter: This method does not take any argument as a parameter.
Return Value: This method returns the locale used in this message format object.
Below are the examples to illustrate the getLocale() method:
Example 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);
    }
}


Output: 

Locale is : en_US

 

Example 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);
    }
}


Output: 

Locale is : zh-hant-tw

 

Reference: https://docs.oracle.com/javase/9/docs/api/java/text/MessageFormat.html#getLocale–
 

RELATED ARTICLES

Most Popular

Dominic
32267 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6635 POSTS0 COMMENTS
Nicole Veronica
11801 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11865 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7026 POSTS0 COMMENTS
Thapelo Manthata
6703 POSTS0 COMMENTS
Umr Jansen
6720 POSTS0 COMMENTS