Formatter locale() method in Java with Examples Last Updated : 01 Apr, 2019 Summarize Comments Improve Suggest changes Share 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() Comment More infoAdvertise with us Next Article Formatter locale() method in Java with Examples G gopaldave Follow Improve Article Tags : Java Java - util package Java-Functions Java-Formatter Practice Tags : Java Similar Reads LocalTime format() method in Java with Examples The format() method of a LocalTime class is used to format this time using the specified formatter passed as a parameter. This method formats this time based on passed formatter to a string. Syntax: public String format(DateTimeFormatter formatter) Parameters: This method accepts a single parameter 2 min read Locale getCountry() Method in Java with Examples The getCountry() method of Locale class in Java is used to get the country or region code for the specified locale. This will either be an empty string or an uppercase ISO 3166 2-letter code, or a UN M.49 3-digit code. Syntax: LOCALE.getCountry() Parameters: This method does not take any parameters. 2 min read Locale getVariant() Method in Java with Examples The getVariant() method of Locale class in Java is used to get a variant code for the specified locale. It returns an empty string if nothing is specified Syntax: LOCALE.getLanguage() Parameters: This method does not take any parameters. Return Value: This method either returns an empty string or th 1 min read Locale hashCode() Method in Java with Examples The hashCode() method of Locale class in Java is used to return the hash code for this locale. Syntax: LOCALE.hashCode() Parameters: This method does not take any parameters. Return Value: This method either returns a hash code value for the given locale. Below programs illustrate the working of has 1 min read Locale getLanguage() Method in Java with Examples The getLanguage() method of Locale class in Java is used to get language code for the specified locale. This will either be an empty string or a lowercase ISO 639 code. Syntax: LOCALE.getLanguage() Parameters: This method does not take any parameters. Return Value: This method either returns an empt 1 min read Like