Category: 10. Java Internationalization

https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRcUEH_0xlWFYgGpeNRUmn5taquEq7H2pEiaw&s

  • DateFormat Class

    java.text.DateFormat class formats dates as per the locale. As different coutries use different formats to display dates. This class is extremely useful in dealing with dates in Internationalization of application. Following example show how to create and use DateFormat Class.

    Example

    Open Compiler

    importjava.text.DateFormat;importjava.util.Date;importjava.util.Locale;publicclassI18NTester{publicstaticvoidmain(String[] args){Locale locale =newLocale("da","DK");DateFormat dateFormat =DateFormat.getDateInstance();System.out.println(dateFormat.format(newDate()));
    
    
      dateFormat =DateFormat.getDateInstance(DateFormat.DEFAULT, locale);System.out.println(dateFormat.format(newDate()));}}</code></pre>

    Output

    It will print the following result.

    Nov 29, 2017
    29-11-2017
    
  • Grouping Digits

    Using setGroupingSize() method of DecimalFormat, default grouping of numbers can be changed. Following example is illustrating the same.

    Example

    Open Compiler

    importjava.text.DecimalFormat;publicclassI18NTester{publicstaticvoidmain(String[] args){double number =121223232473.4567;DecimalFormat decimalFormat =newDecimalFormat();System.out.println(number);System.out.println(decimalFormat.format(number));   
    
    
      decimalFormat.setGroupingSize(4);System.out.println(decimalFormat.format(number));}}</code></pre>

    Output

    It will print the following result.

    1.212232324734567E11
    121,223,232,473.457
    1212,2323,2473.457
    
  • DecimalFormatSymbols Class

    Using DecimalFormatSymbols class, the default separator symbols, grouping separator symbols etc. can be changed. Following example is illustrating the same.

    Example

    Open Compiler

    importjava.text.DecimalFormat;importjava.text.DecimalFormatSymbols;publicclassI18NTester{publicstaticvoidmain(String[] args){String pattern ="#,###.###";double number =126473.4567;DecimalFormat decimalFormat =newDecimalFormat(pattern);System.out.println(decimalFormat.format(number));DecimalFormatSymbols decimalFormatSymbols =newDecimalFormatSymbols();
    
      decimalFormatSymbols.setDecimalSeparator(';');
      decimalFormatSymbols.setGroupingSeparator(':');
      decimalFormat =newDecimalFormat(pattern, decimalFormatSymbols);System.out.println(decimalFormat.format(number));}}</code></pre>

    Output

    It will print the following result.

    126,473.457
    126:473;457
    
  • Locale Specific DecimalFormat

    By default, DecimalFormat object is using the JVM’s locale. We can change the default locale while creating the DecimalFormat object using NumberFormat class. In the example below, we’ll use same pattern for two different locale and you can spot the difference in the output.

    Example

    Open Compiler

    importjava.text.DecimalFormat;importjava.text.NumberFormat;importjava.util.Locale;publicclassI18NTester{publicstaticvoidmain(String[] args){String pattern ="###.##";double number =123.45;Locale enlocale  =newLocale("en","US");Locale dalocale  =newLocale("da","DK");DecimalFormat decimalFormat =(DecimalFormat)NumberFormat.getNumberInstance(enlocale);
    
      decimalFormat.applyPattern(pattern);System.out.println(decimalFormat.format(number));   
      decimalFormat =(DecimalFormat)NumberFormat.getNumberInstance(dalocale);
      decimalFormat.applyPattern(pattern);System.out.println(decimalFormat.format(number));}}</code></pre>

    Output

    It will print the following result.

    123.45
    123,45
    
  • Formatting Patterns

    Followings is the use of characters in formatting patterns.

    Sr.No.Class & Description
    10To display 0 if less digits are present.
    2#To display digit ommitting leading zeroes.
    3.Decimal separator.
    4,Grouping separator.
    5EMantissa and Exponent separator for exponential formats.
    6;Format separator.
    7Negative number prefix.
    8%Shows number as percentage after multiplying with 100.
    9?Shows number as mille after multiplying with 1000.
    10XTo mark character as number prefix/suffix.
    11To mark quote around special characters.

    Example

    In this example, we’re formatting numbers based on different patterns.

    Open Compiler

    importjava.text.DecimalFormat;publicclassI18NTester{publicstaticvoidmain(String[] args){String pattern ="###.###";double number =123456789.123;DecimalFormat numberFormat =newDecimalFormat(pattern);System.out.println(number);//pattern ###.###System.out.println(numberFormat.format(number));//pattern ###.#
    
      numberFormat.applyPattern("###.#");System.out.println(numberFormat.format(number));//pattern ###,###.##
      numberFormat.applyPattern("###,###.##");System.out.println(numberFormat.format(number));
      number =9.34;//pattern 000.###
      numberFormat.applyPattern("000.##");System.out.println(numberFormat.format(number));}}</code></pre>

    Output

    It will print the following result.

    1.23456789123E8
    123456789.123
    123456789.1
    123,456,789.12
    009.34
    
  • DecimalFormat Class

    The java.text.DecimalFormat class is used for formatting numbers as per customized format and as per locale.

    Example – Format Numbers

    In this example, we’re formatting numbers based on a given pattern.

    Open Compiler

    importjava.text.DecimalFormat;publicclassI18NTester{publicstaticvoidmain(String[] args){String pattern ="####,####.##";double number =123456789.123;DecimalFormat numberFormat =newDecimalFormat(pattern);System.out.println(number);System.out.println(numberFormat.format(number));}}

    Output

    It will print the following result.

    1.23456789123E8
    1,2345,6789.12
    
  • Parse Numbers

    Example

    In this example, we’re showcasing parsing of number present in different locale.

    Open Compiler

    importjava.text.NumberFormat;importjava.text.ParseException;importjava.util.Locale;publicclassI18NTester{publicstaticvoidmain(String[] args)throwsParseException{Locale enLocale =newLocale("en","US");Locale daLocale =newLocale("da","DK");NumberFormat numberFormat =NumberFormat.getInstance(daLocale);System.out.println(numberFormat.parse("100,76"));
    
    
      numberFormat =NumberFormat.getInstance(enLocale);System.out.println(numberFormat.parse("100,76"));}}</code></pre>

    Output

    It will print the following result.

    100.76
    10076
    
  • Set Rounding Mode

    Example

    In this example, we’re showcasing Rounding Mode.

    Open Compiler

    importjava.math.RoundingMode;importjava.text.NumberFormat;importjava.util.Locale;publicclassI18NTester{publicstaticvoidmain(String[] args){Locale enLocale =newLocale("en","US");NumberFormat numberFormat =NumberFormat.getInstance(enLocale);
    
    
      numberFormat.setMinimumFractionDigits(0);
      numberFormat.setMaximumFractionDigits(0);System.out.println(numberFormat.format(99.50));
      numberFormat.setRoundingMode(RoundingMode.HALF_DOWN);System.out.println(numberFormat.format(99.50));}}</code></pre>

    Output

    It will print the following result.

    100
    99
    
  • Example

    In this example, we’re setting min and max digits for both integer as well as fractional part of a number.

    Open Compiler

    importjava.text.NumberFormat;importjava.util.Locale;publicclassI18NTester{publicstaticvoidmain(String[] args){Locale enLocale =newLocale("en","US");NumberFormat numberFormat =NumberFormat.getInstance(enLocale);
    
      numberFormat.setMinimumIntegerDigits(2);
      numberFormat.setMaximumIntegerDigits(3);
      numberFormat.setMinimumFractionDigits(2);
      numberFormat.setMaximumFractionDigits(3);System.out.println(numberFormat.format(12234.763443));}}</code></pre>

    Output

    It will print the following result.

    234.763
    
  • Format Percentages

    Example

    In this example, we’re formatting numbers in percentage format.

    Open Compiler

    importjava.text.NumberFormat;importjava.util.Locale;publicclassI18NTester{publicstaticvoidmain(String[] args){Locale enLocale =newLocale("en","US");NumberFormat numberFormat =NumberFormat.getPercentInstance(enLocale);System.out.println(numberFormat.format(0.76));}}

    Output

    It will print the following result.

    76%