How do I set the default locale in the JVM?
From the Oracle Reference:
The default locale of your application is determined in three ways.
First, unless you have explicitly changed the default, the
Locale.getDefault()
method returns the locale that was initially determined
by the Java Virtual Machine (JVM) when it first loaded. That is, the
JVM determines the default locale from the host environment. The host
environment's locale is determined by the host operating system and
the user preferences established on that system.Second, on some Java runtime implementations, the application user can
override the host's default locale by providing this information on
the command line by setting theuser.language
,user.country
, and
user.variant
system properties.Third, your application can call the
Locale.setDefault(Locale)
method. The setDefault(Locale aLocale) method lets your application
set a systemwide (actually VM-wide) resource. After you set the default locale with this
method, subsequent calls to Locale.getDefault() will return the newly
set locale.
Setting java locale settings
I believe java gleans this from the environment variables in which it was launched, so you'll need to make sure your LANG and LC_* environment variables are set appropriately.
The locale manpage has full info on said environment variables.
How do I change the default locale settings in Java to make them consistent?
Nice, i was interested about the same thing but so far I have only a partial solution:
user.timezone=UTC
user.country=CA
user.variant=
user.language=en
file.encoding=utf8
You can use Canada, because it one of the countries using the ISO 8601 format.
Java 7 default locale
This is as designed. Java 7 has changed the way Locale.getDefault()
works. A defect has been entered with Oracle, but they basically said this is As Designed.
To sum up, you must modify the Display Language of the OS. Modifying the Region Format only is no longer sufficient.
Read the bug report here: Locale.getDefault() returns wrong Locale for Java SE 7
How can Locale.setDefault(new Locale (en, US)) be changed at runtime?
The Locale.setDefault
is a global thing. If you have two users that need to use differnt locales that won't work out.
You should probably put the locale in the HttpServletRequest.getSession()
.
After we did the global setDefault it even switched the language and logging of our application server.
Locale.getDefault() returns en always
In Linux/Unix/Mac, the settings LC_ALL
and LANG
can control the default locale for Java programs. In Windows, the locales are set from the Control Panel, under Regional and Language Options.
When the JVM starts in a *nix environment, it will do this:
- Scan the environment for
LC_ALL
- If
LC_ALL
doesn't exist, scan the environment forLANG
- If the JVM setting
user.language
is set, use that in place of the environment variables. - If nothing is set, default to
en_US
(I believe this is the final failure case)
In your environment, you have LC_ALL
set to C
, which is just the C locale. It's basically a traditional fallback to the days when locales weren't used.
You can change LC_ALL
in your case, and restart your JVM, and you should get a new value for java.util.Locale.getDefault()
.
Example:
import java.util.Locale;
public class LocaleTest {
public static void main(String[] args) {
System.out.println(Locale.getDefault());
}
}
Here's running:
> LC_ALL=en_UK java LocaleTest
en_UK
> LC_ALL=ja_JP java LocaleTest
ja_JP
Also note that if you're running Java 1.7.0-b147, there is a bug with the JRE not recognizing environment settings for locale, and will always use the default system locale.
Bug report here: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7073906
Related Topics
Xml Element with Attribute and Content Using Jaxb
How to Pass Parameter to Jsp:Include via C:Set? What Are the Scopes of the Variables in Jsp
Jframe.Dispose() VS System.Exit()
Java User.Home Is Being Set to %Userprofile% and Not Being Resolved
How to Make a List with Checkboxes in Java Swing
What Does Inputstream.Available() Do in Java
How to Send List of Objects to View and Back to Post Method in Controller
Implementing Custom Methods of Spring Data Repository and Exposing Them Through Rest
How to Select an Item from a Dropdown List Using Selenium Webdriver with Java
Why Are New Java.Util.Arrays Methods in Java 8 Not Overloaded for All the Primitive Types
@Preupdate and @Prepersist in Hibernate/JPA (Using Session)
Automatic Reserved Word Escaping for Hibernate Tables and Columns