Open In App

Formatter locale() method in Java with Examples

Last Updated : 01 Apr, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report
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
// 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
// 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()

Next Article
Practice Tags :

Similar Reads