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