5.7. Format text for localization by using NumberFormat and DateFormat - Source Code

Code:

package c5.s7;

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

/**
 * @author Mikalai Zaikin
 */
public class DateFormatDemo {

    public static void main(String... args) {
        Date d = new Date();

        Locale l = Locale.US;
        DateFormat formatter = DateFormat.getDateInstance(DateFormat.DEFAULT, l);
        System.out.println(String.format("Locale: %s; Date: %s", l, formatter.format(d)));

        l = new Locale("be", "BY");
        formatter = DateFormat.getDateInstance(DateFormat.DEFAULT, l);
        System.out.println(String.format("Locale: %s; Date: %s", l, formatter.format(d)));

        l = new Locale("pl", "PL");
        formatter = DateFormat.getDateInstance(DateFormat.DEFAULT, l);
        System.out.println(String.format("Locale: %s; Date: %s", l, formatter.format(d)));

        String pattern = "EEE, MMMM d, yyyy";
        l = Locale.US;
        SimpleDateFormat sdf  = new SimpleDateFormat(pattern, l);
        System.out.println(String.format("Locale: %s; custom date format: %s", l, sdf.format(d)));

        l = new Locale("be", "BY");
        sdf  = new SimpleDateFormat(pattern, l);
        System.out.println(String.format("Locale: %s; custom date format: %s", l, sdf.format(d)));

        l = new Locale("pl", "PL");
        sdf  = new SimpleDateFormat(pattern, l);
        System.out.println(String.format("Locale: %s; custom date format: %s", l, sdf.format(d)));
    }
}
					

output:

Locale: en_US; Date: Apr 6, 2012
Locale: be_BY; Date: 6.4.2012
Locale: pl_PL; Date: 2012-04-06
Locale: en_US; custom date format: Fri, April 6, 2012
Locale: be_BY; custom date format: пт, красавіка 6, 2012
Locale: pl_PL; custom date format: Pt, kwiecień 6, 2012
					

Code:

package c5.s7;

import java.text.NumberFormat;
import java.util.Locale;

/**
 * @author Mikalai Zaikin
 */
public class NumberFormatDemo {

    public static void main(String... args) {
        Integer i = 15_091_974;

        Locale l = Locale.US;
        NumberFormat formatter = NumberFormat.getNumberInstance(l);
        System.out.println(String.format("Locale: %s; int: %s", l, formatter.format(i)));

        l = new Locale("be", "BY");
        formatter = NumberFormat.getNumberInstance(l);
        System.out.println(String.format("Locale: %s; int: %s", l, formatter.format(i)));


        l = Locale.US;
        formatter = NumberFormat.getCurrencyInstance(l);
        System.out.println(String.format("Locale: %s; currency: %s", l, formatter.format(i)));

        l = new Locale("be", "BY");
        formatter = NumberFormat.getCurrencyInstance(l);
        System.out.println(String.format("Locale: %s; currency: %s", l, formatter.format(i)));
    }
}
					

output:

Locale: en_US; int: 15,091,974
Locale: be_BY; int: 15 091 974
Locale: en_US; currency: $15,091,974.00
Locale: be_BY; currency: Руб15 091 974
					

Professional hosting         Free 'Oracle Certified Expert Web Services Developer 6' Guide     Free SCDJWS 5.0 Guide