Friday, October 3, 2025
HomeLanguagesJavaFormatter locale() method in Java with Examples

Formatter locale() method in Java with Examples

The locale() method is a built-in method of the java.util.Formatter which returns a locale. This locale is set by the formatter construction. The format method for this object which has a locale argument does not change this value.

Syntax:

public Locale locale()

Parameters: The function accepts no parameter.

Return Value: The function returns null if no localization is applied, otherwise a locale which has been initialized to the formatter.

Exceptions: The function throws FormatterClosedException if the formatter has been closed before the function call.

Below is the implementation of the above function:

Program 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());
    }
}


Output:

What is your name? 
My name is Gopal Dave !
Locale: en_CA

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


Output:

What is your name? 
My name is Gopal Dave !
Exception is: java.util.FormatterClosedException

Reference: https://docs.oracle.com/javase/10/docs/api/java/util/Formatter.html#locale()

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

Most Popular

Dominic
32332 POSTS0 COMMENTS
Milvus
85 POSTS0 COMMENTS
Nango Kala
6703 POSTS0 COMMENTS
Nicole Veronica
11868 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11929 POSTS0 COMMENTS
Shaida Kate Naidoo
6819 POSTS0 COMMENTS
Ted Musemwa
7080 POSTS0 COMMENTS
Thapelo Manthata
6775 POSTS0 COMMENTS
Umr Jansen
6776 POSTS0 COMMENTS