Formatter locale() method in Java with Examples
Last Updated :
01 Apr, 2019
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:
import java.util.Formatter;
import java.util.Locale;
public class Main {
public static void main(String[] args)
{
StringBuffer buffer
= new StringBuffer();
Formatter frmt
= new Formatter(buffer,
Locale.CANADA);
String name = "My name is Gopal Dave" ;
frmt.format( "What is your name? \n%s !" ,
name);
System.out.println(frmt);
System.out.println( "Locale: "
+ frmt.locale());
}
}
|
Output:
What is your name?
My name is Gopal Dave !
Locale: en_CA
Program 2:
import java.util.Formatter;
import java.util.Locale;
public class Main {
public static void main(String[] args)
{
try {
StringBuffer buffer
= new StringBuffer();
Formatter frmt
= new Formatter(buffer,
Locale.CANADA);
String name = "My name is Gopal Dave" ;
frmt.format( "What is your name? \n%s !" ,
name);
System.out.println(frmt);
frmt.close();
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()
Share your thoughts in the comments
Please Login to comment...