1 /*
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 
17 import java.text.DateFormat;
18 import java.text.DateFormatSymbols;
19 import java.text.Normalizer;
20 import java.util.Arrays;
21 import java.util.Calendar;
22 import java.util.Currency;
23 import java.util.Date;
24 import java.util.Locale;
25 import java.util.MissingResourceException;
26 import java.util.TimeZone;
27 
28 /**
29  * Exercise some locale-table-driven stuff.
30  */
31 public class Main {
32 
main(String[] args)33     public static void main(String[] args) {
34         try {
35             testCalendar();
36         } catch (Exception ex) {
37             ex.printStackTrace(System.out);
38         }
39 
40         try {
41             testDateFormatSymbols();
42         } catch (Exception ex) {
43             ex.printStackTrace(System.out);
44         }
45 
46         try {
47             testCurrency();
48         } catch (Exception ex) {
49             ex.printStackTrace(System.out);
50         }
51 
52         try {
53             testNormalizer();
54         } catch (Exception ex) {
55             ex.printStackTrace(System.out);
56         }
57 
58         try {
59             testIso3();
60         } catch (Exception ex) {
61             ex.printStackTrace(System.out);
62         }
63     }
64 
testCalendar()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 
testDateFormatSymbols()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 
testCurrency()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 
testNormalizer()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)) {
128             System.out.println("Bad decompose: '" + composed + "' --> '"
129                 + res + "'");
130         }
131 
132         res = Normalizer.normalize(decomposed, Normalizer.Form.NFC);
133         if (!composed.equals(res)) {
134             System.out.println("Bad compose: '" + decomposed + "' --> '"
135                 + 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      */
testIso3()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) {
156             System.out.println("couldn't get iso3 language");
157         }
158     }
159 }
160