Step By Step(Java 国际化篇)(二)

2014-11-24 02:47:47 · 作者: · 浏览: 4
00");
21 NumberFormat nf = NumberFormat.getCurrencyInstance();
22 for (int i = 0; i < 12; i++) {
23 BigDecimal interest = balance.multiply(monthlyRate);
24 balance = balance.add(interest);
25 buffer.append("Balance : " + nf.format(balance.doubleva lue()) + "\n");
26 }
27 System.out.println(buffer.toString());
28 }
29 //格式化数字格式中主要的3中表示方式:数字、货币和百分比
30 public class MyTest {
31 static public void displayNumber(Locale currentLocale) {
32 Integer quantity = new Integer(123456);
33 Double amount = new Double(345987.246);
34 NumberFormat numberFormatter;
35 String quantityOut;
36 String amountOut;
37 numberFormatter = NumberFormat.getNumberInstance(currentLocale);
38 quantityOut = numberFormatter.format(quantity);
39 amountOut = numberFormatter.format(amount);
40 System.out.println(quantityOut + " " + currentLocale.toString());
41 System.out.println(amountOut + " " + currentLocale.toString());
42 }
43
44 static public void displayCurrency(Locale currentLocale) {
45 Double currency = new Double(9876543.21);
46 NumberFormat currencyFormatter;
47 String currencyOut;
48 currencyFormatter = NumberFormat.getCurrencyInstance(currentLocale);
49 currencyOut = currencyFormatter.format(currency);
50 System.out.println(currencyOut + " " + currentLocale.toString());
51 }
52
53 static public void displayPercent(Locale currentLocale) {
54 Double percent = new Double(0.75);
55 NumberFormat percentFormatter;
56 String percentOut;
57 percentFormatter = NumberFormat.getPercentInstance(currentLocale);
58 percentOut = percentFormatter.format(percent);
59 System.out.println(percentOut + " " + currentLocale.toString());
60 }
61
62 public static void main(String[] args) {
63 Locale[] locales = { new Locale("fr", "FR"),
64 new Locale("de", "DE"),
65 new Locale("en", "US") };
66 for (int i = 0; i < locales.length; i++) {
67 System.out.println();
68 displayNumber(locales[i]);
69 displayCurrency(locales[i]);
70 displayPercent(locales[i]);
71 }
72 }
73 }
3. 日历格式:
1 //从不同的locale获取每周中的哪一天为周的第一天,并显示
2 //不同locale针对星期的显示名称。
3 public static void main(String[] args) {
4 Locale usersLocale = Locale.getDefault();
5
6 DateFormatSymbols dfs = new DateFormatSymbols(usersLocale);
7 String weekdays[] = dfs.getWeekdays();
8 Calendar cal = Calendar.getInstance(usersLocale);
9 //获取不同地区定义的哪一天是每周的第一天
10 int firstDayOfWeek = cal.getFirstDayOfWeek();
11 int dayOfWeek;
12 //根据不同的每周第一天,遍历并显示不同地区针对星期的名称。
13 for (dayOfWeek = firstDayOfWeek; dayOfWeek < weekdays.length; dayOfWeek++)
14 System.out.println(weekdays[dayOfWeek]);
15
16 for (dayOfWeek = 0; dayOfWeek < firstDayOfWeek; dayOfWeek++)
17 System.out.println(weekdays[dayOfWeek]);
18 }
19 //比较日本时区时间和本地时区时间
20 public static void main(St