1741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes/* 2741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes * Copyright (C) 2012 The Android Open Source Project 3741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes * 4741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes * Licensed under the Apache License, Version 2.0 (the "License"); 5741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes * you may not use this file except in compliance with the License. 6741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes * You may obtain a copy of the License at 7741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes * 8741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes * http://www.apache.org/licenses/LICENSE-2.0 9741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes * 10741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes * Unless required by applicable law or agreed to in writing, software 11741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes * distributed under the License is distributed on an "AS IS" BASIS, 12741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes * See the License for the specific language governing permissions and 14741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes * limitations under the License. 15741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes */ 16741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes 17741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughesimport java.text.DateFormat; 18741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughesimport java.text.DateFormatSymbols; 19741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughesimport java.text.Normalizer; 20741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughesimport java.util.Arrays; 21741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughesimport java.util.Calendar; 22741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughesimport java.util.Currency; 23741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughesimport java.util.Date; 24741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughesimport java.util.Locale; 25741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughesimport java.util.MissingResourceException; 26741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughesimport java.util.TimeZone; 27741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes 28741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes/** 29741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes * Exercise some locale-table-driven stuff. 30741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes */ 31741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughespublic class Main { 32741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes 33741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes public static void main(String[] args) { 34741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes try { 35741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes testCalendar(); 36741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes } catch (Exception ex) { 37741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes ex.printStackTrace(); 38741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes } 39741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes 40741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes try { 41741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes testDateFormatSymbols(); 42741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes } catch (Exception ex) { 43741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes ex.printStackTrace(); 44741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes } 45741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes 46741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes try { 47741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes testCurrency(); 48741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes } catch (Exception ex) { 49741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes ex.printStackTrace(); 50741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes } 51741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes 52741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes try { 53741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes testNormalizer(); 54741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes } catch (Exception ex) { 55741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes ex.printStackTrace(); 56741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes } 57741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes 58741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes try { 59741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes testIso3(); 60741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes } catch (Exception ex) { 61741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes ex.printStackTrace(); 62741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes } 63741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes } 64741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes 65741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes static void testCalendar() { 66741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes TimeZone tz = TimeZone.getTimeZone("GMT"); 67741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes 68741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes Locale usa = new Locale("en", "US"); 69741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes Calendar usaCal = Calendar.getInstance(tz, usa); 70741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes usaCal.clear(); // don't want current date/time 71741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes usaCal.set(2012, Calendar.JANUARY, 1); 72741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes 73741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes Date when = usaCal.getTime(); 74741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes DateFormat fmt = DateFormat.getDateInstance(DateFormat.FULL, usa); 75741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes fmt.setTimeZone(tz); // defaults to local TZ; force GMT 76741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes System.out.println("USA(" + fmt.getTimeZone().getID() + "): " 77741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes + fmt.format(when)); 78741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes 79741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes System.out.println("USA: first=" 80741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes + usaCal.getFirstDayOfWeek() + ", name=" 81741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes + usaCal.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, usa)); 82741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes 83741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes 84741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes Locale france = new Locale("fr", "FR"); 85741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes Calendar franceCal = Calendar.getInstance(tz, france); 86741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes franceCal.clear(); 87741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes franceCal.set(2012, Calendar.JANUARY, 2); 88741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes 89741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes when = franceCal.getTime(); 90741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes fmt = DateFormat.getDateInstance(DateFormat.FULL, usa); 91741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes fmt.setTimeZone(tz); // defaults to local TZ; force GMT 92741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes System.out.println("France(" + fmt.getTimeZone().getID() + "): " 93741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes + fmt.format(when)); 94741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes 95741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes System.out.println("France: first=" 96741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes + franceCal.getFirstDayOfWeek() + ", name=" 97741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes + franceCal.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, france)); 98741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes } 99741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes 100741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes static void testDateFormatSymbols() { 101741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes Locale usa = new Locale("en", "US"); 102741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes DateFormatSymbols syms = DateFormatSymbols.getInstance(usa); 103741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes String[] list = syms.getAmPmStrings(); 104741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes System.out.println("USA dfs: " + Arrays.deepToString(list)); 105741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes } 106741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes 107741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes static void testCurrency() { 108741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes Locale usa = new Locale("en", "US"); 109741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes Currency dollars = Currency.getInstance(usa); 110741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes 111741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes System.out.println(usa.toString() + ": " + dollars.toString() 112741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes + " " + dollars.getSymbol() + dollars.getDefaultFractionDigits()); 113741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes 114741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes Locale japan = new Locale("jp", "JP"); 115741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes Currency yen = Currency.getInstance(japan); 116741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes 117741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes System.out.println(japan.toString() + ": " + yen.toString() 118741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes + " " + yen.getSymbol() + yen.getDefaultFractionDigits()); 119741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes } 120741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes 121741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes static void testNormalizer() { 122741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes String composed = "Bl\u00c1ah"; 123741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes String decomposed = "Bl\u0041\u0301ah"; 124741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes String res; 125741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes 126741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes res = Normalizer.normalize(composed, Normalizer.Form.NFD); 127741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes if (!decomposed.equals(res)) { 128741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes System.err.println("Bad decompose: '" + composed + "' --> '" 129741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes + res + "'"); 130741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes } 131741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes 132741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes res = Normalizer.normalize(decomposed, Normalizer.Form.NFC); 133741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes if (!composed.equals(res)) { 134741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes System.err.println("Bad compose: '" + decomposed + "' --> '" 135741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes + res + "'"); 136741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes } 137741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes 138741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes System.out.println("Normalizer passed"); 139741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes } 140741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes 141741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes /* 142741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes * Test that we can set and get an ISO3 language code. Support for this 143741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes * is expected by the Android framework. 144741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes */ 145741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes static void testIso3() { 146741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes Locale loc; 147741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes loc = new Locale("en", "US"); 148741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes System.out.println("loc: " + loc); 149741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes System.out.println(" iso3=" + loc.getISO3Language()); 150741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes 151741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes loc = new Locale("eng", "USA"); 152741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes System.out.println("loc: " + loc); 153741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes try { 154741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes System.out.println(" iso3=" + loc.getISO3Language()); 155741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes } catch (MissingResourceException mre) { 156741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes System.err.println("couldn't get iso3 language"); 157741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes } 158741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes } 159741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes} 160