Setting the Default Locale - Java Internationalization

Java examples for Internationalization:Locale

Introduction

Use the Locale.setDefault() method to set a Locale by default.

Demo Code

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.ResourceBundle;

public class Main {
  public static void main(String[] args) {
    Locale.setDefault(Locale.FRANCE);
    demoDefaultLocaleSettings();//from  w ww.ja v  a2s . c  om

    Locale.setDefault(Locale.Category.DISPLAY, Locale.forLanguageTag("es-MX"));
    Locale.setDefault(Locale.Category.FORMAT, Locale.US);
    demoDefaultLocaleSettings();

    Locale.setDefault(Locale.Category.DISPLAY, Locale.US);
    Locale.setDefault(Locale.Category.FORMAT, Locale.forLanguageTag("es-MX"));
    demoDefaultLocaleSettings();

    Locale.setDefault(Locale.US);
    demoDefaultLocaleSettings();
  }

  public static void demoDefaultLocaleSettings() {
    DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT,
        DateFormat.SHORT);
    ResourceBundle resource = ResourceBundle.getBundle("SimpleResources",
        Locale.getDefault(Locale.Category.DISPLAY));
    String greeting = resource.getString("GOOD_MORNING");
    String date = df.format(new Date());
    System.out.printf("DEFAULT LOCALE: %s\n", Locale.getDefault());
    System.out.printf("DISPLAY LOCALE: %s\n",
        Locale.getDefault(Locale.Category.DISPLAY));
    System.out.printf("FORMAT LOCALE:  %s\n",
        Locale.getDefault(Locale.Category.FORMAT));
    System.out.printf("%s, %s\n\n", greeting, date);
  }

}

Related Tutorials