4

UPDATE: Putting a working/fixed code here just for the sake of completeness. Detailed explanation of why this is happening can be found here. Thanks to user Perception for pointing me to it.

Here is the code:

import java.util.Locale;

/**
 * Demonstrating proper usage of Java's {@linkplain Locale} class
 * in respect to accessing user's regional settings, specifically
 * the locale configured for date/time/number formatting.
 *
 * @see Locale#getDefault(java.util.Locale.Category)
 * @see Locale#getDefault()
 */
public class DefaultLocaleChangesJava7Demo {
  private static final String NO_VALUE = "<- empty ->";
  private static final String[] ENVVAR_NAMES = {
      "java.runtime.name", "java.runtime.version", "java.vm.name", "java.vm.version",
      "os.name", "os.version", "os.arch",
      "user.language", "user.country", "user.script", "user.variant",
      "user.language.format", "user.country.format", "user.script.format" };

  public static void main(String[] args) {
    // Print out select environment variables.
    System.out.println();
    System.out.println("System Information (environment):");
    for (String name : ENVVAR_NAMES) {
      String value = System.getProperty(name);
      if (value == null || value.length() == 0) {
        value = NO_VALUE;
      }
      System.out.println("  " + name + " = " + value);
    }

    // Print out default display locale information.
    System.out.println("\nDefault Display Locale:");
    final Locale defaultLocale = Locale.getDefault();
    System.out.println(localeToString(defaultLocale, "  default."));

    // Print out default format locale information.
    System.out.println("\nDefault Format Locale:");
    final Locale formatLocale = Locale.getDefault(Locale.Category.FORMAT);
    System.out.println(localeToString(formatLocale, "  format."));
  }

  public static String localeToString(Locale locale, String prefix) {
    final StringBuffer result = new StringBuffer();

    result.append(prefix);
    result.append("locale.language = ");
    result.append(locale.getLanguage());
    result.append(" (");
    result.append(locale.getDisplayLanguage());
    result.append(")\n");

    result.append(prefix);
    result.append("locale.country = ");
    result.append(locale.getCountry());
    result.append(" (");
    result.append(locale.getDisplayCountry());
    result.append(")\n");

    result.append(prefix);
    result.append("locale.script = ");
    result.append(locale.getScript());
    result.append(" (");
    result.append(locale.getDisplayScript());
    result.append(")\n");

    result.append(prefix);
    result.append("locale.variant = ");
    result.append(locale.getVariant());
    result.append(" (");
    result.append(locale.getDisplayVariant());
    result.append(")\n");

    return result.toString();
  }
}

Original Question

Today I noticed that Java's Locale.getDefault() is not returning the regional settings I configured in my Windows 7 account.

Demo application:

import java.util.Locale;

public class BadLocaleDemo {
  private static final String NO_VALUE = "<- empty ->";
  private static final String[] ENVVAR_NAMES = {
      "java.runtime.name", "java.runtime.version", "java.vm.name", "java.vm.version",
      "os.name", "os.version", "os.arch" };

  public static void main(String[] args) {
    // Print out select environment variables.
    System.out.println();
    System.out.println("System Information (environment):");
    for (String name : ENVVAR_NAMES) {
      String value = System.getProperty(name);
      if (value == null || value.length() == 0) {
        value = NO_VALUE;
      }
      System.out.println("  " + name + " = " + value);
    }

    // Print out default locale information.
    System.out.println();
    System.out.println("Default Locale:");

    final Locale defaultLocale = Locale.getDefault();
    System.out.println("  default.locale.language = " + defaultLocale.getLanguage());
    System.out.println("  default.locale.country = " + defaultLocale.getCountry());
    System.out.println("  default.locale.display.language = " + defaultLocale.getDisplayLanguage());
    System.out.println("  default.locale.display.country = " + defaultLocale.getDisplayCountry());
  }
}

And the information it prints out:

System Information (environment):
  java.runtime.name = Java(TM) SE Runtime Environment
  java.runtime.version = 1.7.0_02-b13
  java.vm.name = Java HotSpot(TM) 64-Bit Server VM
  java.vm.version = 22.0-b10
  os.name = Windows 7
  os.version = 6.1
  os.arch = amd64

Default Locale:
  default.locale.language = en
  default.locale.country = US
  default.locale.display.language = English
  default.locale.display.country = United States

Default local should be sr_RS as per my regional settings (names of week days are in cyrilic on the tray clock), but Java is reporting en_US.

Does anyone have an idea how to solve this?

Only relevant posts I found on StackOverflow are these two:

Community
  • 1
  • 1
Cebence
  • 2,406
  • 2
  • 19
  • 20

0 Answers0