Setting the Default Locale in Java
Hello, I am working on java programming and currently confused about the setting of the Default locale in java programming language. I have tried to search this on internet, but I am not able to get the satisfactory solution for it. If anyone is having knowledge about the Default Locale in java then please reply about it. Thank you in advance.
Re: Setting the Default Locale in Java
Locales identify a specific language and geographic region. Locale-sensitive objects use java.util.Locale objects to customize how they present and format data to the user. Locales affect user interface language, case mapping, collation (sorting), date and time formats, and number and currency formats. Locales are critical to many culturally and linguistically sensitive data operations.
A java.util.Locale is a lightweight object that contains only a few important members:
- A language code
- An optional country or region code
- An optional variant code
Retrieve the default locale using the following method:
Code:
public static Locale getDefault()
Re: Setting the Default Locale in Java
If you use the Locale class in java then you will able to get the following methods for use:
- clone()
- equals(Object obj)
- getAvailableLocales()
- getCountry()
- getDefault()
- getDisplayCountry(Locale inLocale)
- getDisplayLanguage()
- getDisplayLanguage(Locale inLocale)
- getDisplayName(Locale inLocale)
- getDisplayVariant(Locale inLocale)
- getISO3Language()
- getISOCountries()
- getISOLanguages()
- getLanguage()
Re: Setting the Default Locale in Java
Hey, you can simply set the default locale in java with the help of following code:
Code:
import java.text.*;
import java.util.Locale;
public class setDeafultLocale
{
public static void main(String[] args)
{
Locale L = Locale.getDefault();
System.out.println("Before setting, Locale is = " + L);
L = Locale.Japan;
Locale.setDefault(L);
System.out.println("After setting, Locale is = " + L);
}
}
Re: Setting the Default Locale in Java
Hello, If you want to change the default locale in java then you can make use of the code below:
Code:
import java.text.DateFormat;
import java.text.NumberFormat;
import java.util.Date;
import java.util.Locale;
public class SetLocale {
public static void main(String[] arg) {
switch (arg.length) {
case 0:
Locale.setDefault(Locale.India);
break;
case 1:
throw new IllegalArgumentException();
case 2:
Locale.setDefault(new Locale(arg[0], arg[1]));
break;
default:
System.out.println("Usage: SetLocale [language [country]]");
}
DateFormat dateformat = DateFormat.getInstance();
NumberFormat numberformat = NumberFormat.getInstance();
System.out.println(dateformat.format(new Date()));
System.out.println(numberformat.format(123.4567));
}
}
Re: Setting the Default Locale in Java
If you want to set the default locale then java help you with the following two APIs:
Locale.getDefault():
If you are using this method then it will simply provide you default locale in all of the locales which you have installed.
Locale.setDefault():
If you make use of the method above then it will directly set the locale for the default setting.