blob: 60c0551b846d741625a5538176926517feac4145 [file] [log] [blame]
Elliott Hughes741b5b72012-01-31 19:18:51 -08001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17import java.text.DateFormat;
18import java.text.DateFormatSymbols;
19import java.text.Normalizer;
20import java.util.Arrays;
21import java.util.Calendar;
22import java.util.Currency;
23import java.util.Date;
24import java.util.Locale;
25import java.util.MissingResourceException;
26import java.util.TimeZone;
27
28/**
29 * Exercise some locale-table-driven stuff.
30 */
31public class Main {
32
33 public static void main(String[] args) {
34 try {
35 testCalendar();
36 } catch (Exception ex) {
Kevin Brodskyf6c66c32015-12-17 14:13:00 +000037 ex.printStackTrace(System.out);
Elliott Hughes741b5b72012-01-31 19:18:51 -080038 }
39
40 try {
41 testDateFormatSymbols();
42 } catch (Exception ex) {
Kevin Brodskyf6c66c32015-12-17 14:13:00 +000043 ex.printStackTrace(System.out);
Elliott Hughes741b5b72012-01-31 19:18:51 -080044 }
45
46 try {
47 testCurrency();
48 } catch (Exception ex) {
Kevin Brodskyf6c66c32015-12-17 14:13:00 +000049 ex.printStackTrace(System.out);
Elliott Hughes741b5b72012-01-31 19:18:51 -080050 }
51
52 try {
53 testNormalizer();
54 } catch (Exception ex) {
Kevin Brodskyf6c66c32015-12-17 14:13:00 +000055 ex.printStackTrace(System.out);
Elliott Hughes741b5b72012-01-31 19:18:51 -080056 }
57
58 try {
59 testIso3();
60 } catch (Exception ex) {
Kevin Brodskyf6c66c32015-12-17 14:13:00 +000061 ex.printStackTrace(System.out);
Elliott Hughes741b5b72012-01-31 19:18:51 -080062 }
63 }
64
65 static void testCalendar() {
66 TimeZone tz = TimeZone.getTimeZone("GMT");
67
68 Locale usa = new Locale("en", "US");
69 Calendar usaCal = Calendar.getInstance(tz, usa);
70 usaCal.clear(); // don't want current date/time
71 usaCal.set(2012, Calendar.JANUARY, 1);
72
73 Date when = usaCal.getTime();
74 DateFormat fmt = DateFormat.getDateInstance(DateFormat.FULL, usa);
75 fmt.setTimeZone(tz); // defaults to local TZ; force GMT
76 System.out.println("USA(" + fmt.getTimeZone().getID() + "): "
77 + fmt.format(when));
78
79 System.out.println("USA: first="
80 + usaCal.getFirstDayOfWeek() + ", name="
81 + usaCal.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, usa));
82
83
84 Locale france = new Locale("fr", "FR");
85 Calendar franceCal = Calendar.getInstance(tz, france);
86 franceCal.clear();
87 franceCal.set(2012, Calendar.JANUARY, 2);
88
89 when = franceCal.getTime();
90 fmt = DateFormat.getDateInstance(DateFormat.FULL, usa);
91 fmt.setTimeZone(tz); // defaults to local TZ; force GMT
92 System.out.println("France(" + fmt.getTimeZone().getID() + "): "
93 + fmt.format(when));
94
95 System.out.println("France: first="
96 + franceCal.getFirstDayOfWeek() + ", name="
97 + franceCal.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, france));
98 }
99
100 static void testDateFormatSymbols() {
101 Locale usa = new Locale("en", "US");
102 DateFormatSymbols syms = DateFormatSymbols.getInstance(usa);
103 String[] list = syms.getAmPmStrings();
104 System.out.println("USA dfs: " + Arrays.deepToString(list));
105 }
106
107 static void testCurrency() {
108 Locale usa = new Locale("en", "US");
109 Currency dollars = Currency.getInstance(usa);
110
111 System.out.println(usa.toString() + ": " + dollars.toString()
112 + " " + dollars.getSymbol() + dollars.getDefaultFractionDigits());
113
114 Locale japan = new Locale("jp", "JP");
115 Currency yen = Currency.getInstance(japan);
116
117 System.out.println(japan.toString() + ": " + yen.toString()
118 + " " + yen.getSymbol() + yen.getDefaultFractionDigits());
119 }
120
121 static void testNormalizer() {
122 String composed = "Bl\u00c1ah";
123 String decomposed = "Bl\u0041\u0301ah";
124 String res;
125
126 res = Normalizer.normalize(composed, Normalizer.Form.NFD);
127 if (!decomposed.equals(res)) {
Kevin Brodskyf6c66c32015-12-17 14:13:00 +0000128 System.out.println("Bad decompose: '" + composed + "' --> '"
Elliott Hughes741b5b72012-01-31 19:18:51 -0800129 + res + "'");
130 }
131
132 res = Normalizer.normalize(decomposed, Normalizer.Form.NFC);
133 if (!composed.equals(res)) {
Kevin Brodskyf6c66c32015-12-17 14:13:00 +0000134 System.out.println("Bad compose: '" + decomposed + "' --> '"
Elliott Hughes741b5b72012-01-31 19:18:51 -0800135 + res + "'");
136 }
137
138 System.out.println("Normalizer passed");
139 }
140
141 /*
142 * Test that we can set and get an ISO3 language code. Support for this
143 * is expected by the Android framework.
144 */
145 static void testIso3() {
146 Locale loc;
147 loc = new Locale("en", "US");
148 System.out.println("loc: " + loc);
149 System.out.println(" iso3=" + loc.getISO3Language());
150
151 loc = new Locale("eng", "USA");
152 System.out.println("loc: " + loc);
153 try {
154 System.out.println(" iso3=" + loc.getISO3Language());
155 } catch (MissingResourceException mre) {
Kevin Brodskyf6c66c32015-12-17 14:13:00 +0000156 System.out.println("couldn't get iso3 language");
Elliott Hughes741b5b72012-01-31 19:18:51 -0800157 }
158 }
159}