Java Locale create from Locale.Builder

Description

Java Locale create from Locale.Builder

import java.text.DateFormat;
import java.text.NumberFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Locale.Builder;

public class Main {

   public static void main(String[] args) {
      String[][] langRegions = { { "fr", "FR" }, { "ja", "JP" }, { "en", "US" } };
      Builder builder = new Builder();
      Locale l = null;//from w  w w  .j  a va 2 s.c  o m
      for (String[] lr : langRegions) {
         builder.clear();
         builder.setLanguage(lr[0]).setRegion(lr[1]);
         l = builder.build();
         displayLocalizedData(l);
      }
   }

   private static void displayLocalizedData(Locale l) {
      long number = 123456789L;
      Date date = new Date();

      NumberFormat nf = NumberFormat.getInstance(l);
      DateFormat df = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, l);
      System.out.printf("Locale: %s\nNumber: %s\nDate: %s\n\n", l.getDisplayName(), nf.format(number), df.format(date));
   }
}



PreviousNext

Related