The Locale Class : Formatter « Utility Classes « SCJP






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

public class MainClass {
  public static void main(String[] argv) {
    Calendar c = Calendar.getInstance();
    c.set(2010, 11, 14); // December 14, 2010
    // (month is 0-based
    Date d2 = c.getTime();

    Locale locIT = new Locale("it", "IT"); // Italy
    Locale locPT = new Locale("pt"); // Portugal
    Locale locBR = new Locale("pt", "BR"); // Brazil
    Locale locIN = new Locale("hi", "IN"); // India
    Locale locJA = new Locale("ja"); // Japan

    DateFormat dfUS = DateFormat.getInstance();
    System.out.println("US       " + dfUS.format(d2));

    DateFormat dfUSfull = DateFormat.getDateInstance(DateFormat.FULL);
    System.out.println("US full  " + dfUSfull.format(d2));

    DateFormat dfIT = DateFormat.getDateInstance(DateFormat.FULL, locIT);
    System.out.println("Italy    " + dfIT.format(d2));

    DateFormat dfPT = DateFormat.getDateInstance(DateFormat.FULL, locPT);
    System.out.println("Portugal " + dfPT.format(d2));

    DateFormat dfBR = DateFormat.getDateInstance(DateFormat.FULL, locBR);
    System.out.println("Brazil   " + dfBR.format(d2));

    DateFormat dfIN = DateFormat.getDateInstance(DateFormat.FULL, locIN);
    System.out.println("India    " + dfIN.format(d2));

    DateFormat dfJA = DateFormat.getDateInstance(DateFormat.FULL, locJA);
    System.out.println("Japan    " + dfJA.format(d2));
  }
}
US       12/14/10 7:05 PM
US full  Tuesday, December 14, 2010
Italy    martedì 14 dicembre 2010
Portugal Terça-feira, 14 de Dezembro de 2010
Brazil   Terça-feira, 14 de Dezembro de 2010
India    ???????, ?? ??????, ????
Japan    2010?12?14?








8.25.Formatter
8.25.1.%b Formats a boolean value (wrapper or primitive)
8.25.2.%c Formats a character
8.25.3.%d Formats an integer
8.25.4.%f Formats a floating-point number
8.25.5.%s Formats an object, generally by calling its toString() method
8.25.6.By default, arguments are formatted in their order of appearance in Formatter.
8.25.7.Stick a number and then a dollar sign ($) immediately after the % sign to set the order
8.25.8.Justified a field with Formatter
8.25.9.Control the number of characters that will appear to the right of the decimal point: %w.df
8.25.10.Internationalization and Formatter
8.25.11.Formatting Dates with java.text.DateFormat
8.25.12.Prints out a date in each of the four formats. (The locale is the U.S.)
8.25.13.DateFormat.getDateInstance() accepts a style and a locale.
8.25.14.Formatting Numbers and Currency
8.25.15.To format currency, call NumberFormat.getCurrencyInstance().
8.25.16.The Locale Class
8.25.17.Create Strings that represent a given locale's country and language in terms of both the default locale and any other locale:
8.25.18.The NumberFormat Class
8.25.19.Use getMaximumFractionDigits(), setMaximumFractionDigits(), parse(), and setParseIntegerOnly():