Java Locale get/set default locale

Description

Java Locale get/set default locale

import java.text.DateFormat;
import java.util.Calendar;
import java.util.Locale;

public class Main {

   public static void main(String[] args) {
      // Using the Locale.Category enumeration to display
      // information using two different locales
      Locale locale = Locale.getDefault();
      Calendar calendar = Calendar.getInstance();
      calendar.setWeekDate(2012, 16, 3);

      System.out.println(DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG).format(calendar.getTime()));
      System.out.println(locale.getDisplayLanguage());

      Locale.setDefault(Locale.Category.FORMAT, Locale.JAPANESE);
      Locale.setDefault(Locale.Category.DISPLAY, Locale.GERMAN);

      System.out.println(DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG).format(calendar.getTime()));
      System.out.println("" + locale.getDisplayLanguage());

   }/*from  w w  w.  j a  v a  2 s  . c o m*/
}
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

public class Main {
   public static void main(String[] args) {
      // Set ALL locales to fr-FR
      Locale.setDefault(Locale.FRANCE);
      demoDefaultLocaleSettings();/*from www. ja  v a2 s.c om*/

      // System default is still fr-FR
      // DISPLAY default is es-MX
      // FORMAT default is en-US
      Locale.setDefault(Locale.Category.DISPLAY, Locale.forLanguageTag("es-MX"));
      Locale.setDefault(Locale.Category.FORMAT, Locale.US);
      demoDefaultLocaleSettings();

      // System default is still fr-FR
      // DISPLAY default is en-US
      // FORMAT default is es-MX
      Locale.setDefault(Locale.Category.DISPLAY, Locale.US);
      Locale.setDefault(Locale.Category.FORMAT, Locale.forLanguageTag("es-MX"));
      demoDefaultLocaleSettings();

      // System default is Locale.US
      // Resets both DISPLAY and FORMAT locales to en-US as well.
      Locale.setDefault(Locale.US);
      demoDefaultLocaleSettings();

   }

   public static void demoDefaultLocaleSettings() {
      Date NOW = new Date();
      DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
      String date = df.format(NOW);
      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(date);
   }
}



PreviousNext

Related