Java by API/java.util/Locale
Содержание
- 1 Locale.CANADA_FRENCH
- 2 Locale.GERMANY
- 3 Locale: getDefault()
- 4 Locale: getDisplayCountry()
- 5 Locale: getDisplayCountry(Locale inLocale)
- 6 Locale: getDisplayLanguage()
- 7 Locale: getDisplayName()
- 8 Locale: getDisplayName(Locale inLocale)
- 9 Locale: getDisplayVariant()
- 10 Locale: getDisplayVariant(Locale inLocale)
- 11 Locale: getISOCountries()
- 12 Locale.getISOLanguages()
- 13 Locale.ITALY
- 14 Locale.JAPAN
- 15 Locale.KOREA
- 16 Locale: setDefault(Locale newLocale)
- 17 Locale.TAIWAN
- 18 Locale.UK
- 19 Locale.US
- 20 new Locale(String language, String country, String variant)
Locale.CANADA_FRENCH
<source lang="java">
import java.util.Locale; public class Main {
public static void main(String args[]) { Locale currentLocale = Locale.getDefault(); Locale locales[] = { Locale.TAIWAN, Locale.KOREA, Locale.ITALY, Locale.CANADA, Locale.CANADA_FRENCH }; System.out.println("CURRENT LOCALE:"); describeLocale(currentLocale); System.out.println("OTHER LOCALES:"); for (int i = 0; i < locales.length; ++i) describeLocale(locales[i]); } static void describeLocale(Locale l) { System.out.println("Country: " + l.getDisplayCountry()); System.out.println("Language: " + l.getDisplayLanguage()); System.out.println(); }
}
</source>
Locale.GERMANY
<source lang="java">
/*Output:
* User"s number (GERMANY): 1.976,083 * */
import java.text.NumberFormat; import java.util.Locale; public class MainClass {
public static void main(String args[]) throws Exception { NumberFormat numberFormat = NumberFormat.getInstance(); numberFormat.setParseIntegerOnly(false); double usersNumber = 1976.0826; numberFormat = NumberFormat.getNumberInstance(Locale.GERMANY); System.out.println("User"s number (GERMANY): " + numberFormat.format(usersNumber)); }
}
</source>
Locale: getDefault()
<source lang="java">
/*
* Copyright (c) Ian F. Darwin, http://www.darwinsys.ru/, 1996-2002. * All rights reserved. Software written by Ian F. Darwin and others. * $Id: LICENSE,v 1.8 2004/02/09 03:33:38 ian Exp $ * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS"" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * * Java, the Duke mascot, and all variants of Sun"s Java "steaming coffee * cup" logo are trademarks of Sun Microsystems. Sun"s, and James Gosling"s, * pioneering role in inventing and promulgating (and standardizing) the Java * language and environment is gratefully acknowledged. * * The pioneering role of Dennis Ritchie and Bjarne Stroustrup, of AT&T, for * inventing predecessor languages C and C++ is also gratefully acknowledged. */
import java.util.Locale; /** Print the default locale */ public class Main {
public static void main(String[] av) { Locale l = Locale.getDefault(); System.out.println("Today"s Locale is " + l); }
}
</source>
Locale: getDisplayCountry()
<source lang="java">
/*
*
The Date for United States:
In FULL is Tuesday, May 9, 2006 In LONG is May 9, 2006 In MEDIUM is May 9, 2006 In SHORT is 5/9/06
The Date for United Kingdom:
In FULL is 09 May 2006 In LONG is 09 May 2006 In MEDIUM is 09-May-2006 In SHORT is 09/05/06
The Date for Germany:
In FULL is Dienstag, 9. Mai 2006 In LONG is 9. Mai 2006 In MEDIUM is 09.05.2006 In SHORT is 09.05.06
The Date for France:
In FULL is mardi 9 mai 2006 In LONG is 9 mai 2006 In MEDIUM is 9 mai 2006 In SHORT is 09/05/06 * */
import java.text.DateFormat; import java.util.Date; import java.util.Locale; public class MainClass {
public static void main(String[] args) { Date today = new Date(); Locale[] locales = { Locale.US, Locale.UK, Locale.GERMANY, Locale.FRANCE }; int[] styles = { DateFormat.FULL, DateFormat.LONG, DateFormat.MEDIUM, DateFormat.SHORT }; DateFormat fmt; String[] styleText = { "FULL", "LONG", "MEDIUM", "SHORT" }; // Output the date for each local in four styles for (int i = 0; i < locales.length; i++) { System.out.println("\nThe Date for " + locales[i].getDisplayCountry() + ":"); for (int j = 0; j < styles.length; j++) { fmt = DateFormat.getDateInstance(styles[j], locales[i]); System.out.println("\tIn " + styleText[j] + " is " + fmt.format(today)); } } }
}
</source>
Locale: getDisplayCountry(Locale inLocale)
<source lang="java">
import java.util.Locale; public class Main {
public static void main(String [] argv) { Locale defaultLocale = Locale.getDefault(); System.out.println(defaultLocale.getISO3Country()); System.out.println(defaultLocale.getDisplayCountry()); System.out.println(defaultLocale.getDisplayCountry(Locale.GERMAN)); }
}
</source>
Locale: getDisplayLanguage()
<source lang="java">
import java.util.Locale; public class Main {
public static void main(String args[]) { Locale currentLocale = Locale.getDefault(); Locale locales[] = { Locale.TAIWAN, Locale.KOREA, Locale.ITALY, Locale.CANADA, Locale.CANADA_FRENCH }; System.out.println("CURRENT LOCALE:"); describeLocale(currentLocale); System.out.println("OTHER LOCALES:"); for (int i = 0; i < locales.length; ++i) describeLocale(locales[i]); } static void describeLocale(Locale l) { System.out.println("Country: " + l.getDisplayCountry()); System.out.println("Language: " + l.getDisplayLanguage()); System.out.println(); }
}
</source>
Locale: getDisplayName()
<source lang="java">
import java.util.Locale; public class MainClass {
public static void main(String args[]) throws Exception { Locale defaultLocale = Locale.getDefault(); System.out.println(defaultLocale.getDisplayName()); System.out.println(defaultLocale.getDisplayName(Locale.ITALIAN)); System.out.println(defaultLocale.getDisplayName(Locale.US)); }
}
</source>
Locale: getDisplayName(Locale inLocale)
<source lang="java">
import java.util.Locale; public class MainClass {
public static void main(String args[]) throws Exception { Locale defaultLocale = Locale.getDefault(); System.out.println(defaultLocale.getDisplayName()); System.out.println(defaultLocale.getDisplayName(Locale.ITALIAN)); System.out.println(defaultLocale.getDisplayName(Locale.US)); }
}
</source>
Locale: getDisplayVariant()
<source lang="java">
import java.util.Locale; public class MainClass {
public static void main(String args[]) throws Exception { Locale defaultLocale = Locale.getDefault(); System.out.println(defaultLocale.getDisplayVariant()); System.out.println(defaultLocale.getDisplayVariant(Locale.US)); System.out.println((new Locale("en", "US", "WIN_TX_Austin")).getDisplayVariant()); }
}
</source>
Locale: getDisplayVariant(Locale inLocale)
<source lang="java">
import java.util.Locale; public class MainClass {
public static void main(String args[]) throws Exception { Locale defaultLocale = Locale.getDefault(); System.out.println(defaultLocale.getDisplayVariant()); System.out.println(defaultLocale.getDisplayVariant(Locale.US)); System.out.println((new Locale("en", "US", "WIN_TX_Austin")).getDisplayVariant()); }
}
</source>
Locale: getISOCountries()
<source lang="java">
import java.util.Locale; public class MainClass {
public static void main(String args[]) throws Exception { String [] countries = Locale.getISOCountries(); System.out.println("Countries:\n"); for (int i = 0; i < countries.length; i++) System.out.println(countries[i]); }
}
</source>
Locale.getISOLanguages()
<source lang="java">
import java.util.Locale; public class MainClass {
public static void main(String args[]) throws Exception { String[] languages = Locale.getISOLanguages(); System.out.println("\nLanguages:\n"); for (int i = 0; i < languages.length; i++) System.out.println(languages[i]); }
}
</source>
Locale.ITALY
<source lang="java">
import java.util.Locale; public class Main {
public static void main(String args[]) { Locale currentLocale = Locale.getDefault(); Locale locales[] = { Locale.TAIWAN, Locale.KOREA, Locale.ITALY, Locale.CANADA, Locale.CANADA_FRENCH }; System.out.println("CURRENT LOCALE:"); describeLocale(currentLocale); System.out.println("OTHER LOCALES:"); for (int i = 0; i < locales.length; ++i) describeLocale(locales[i]); } static void describeLocale(Locale l) { System.out.println("Country: " + l.getDisplayCountry()); System.out.println("Language: " + l.getDisplayLanguage()); System.out.println(); }
}
</source>
Locale.JAPAN
<source lang="java">
/*
* Output:
Japan: 4/28/06 Korea: Apr 28, 2006 United Kingdom: 28 April 2006 United States: Friday, April 28, 2006
* */
import java.text.DateFormat; import java.util.Date; import java.util.Locale; public class MainClass {
public static void main(String args[]) { Date date = new Date(); DateFormat df; df = DateFormat.getDateInstance(DateFormat.SHORT, Locale.JAPAN); System.out.println("Japan: " + df.format(date)); df = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.KOREA); System.out.println("Korea: " + df.format(date)); df = DateFormat.getDateInstance(DateFormat.LONG, Locale.UK); System.out.println("United Kingdom: " + df.format(date)); df = DateFormat.getDateInstance(DateFormat.FULL, Locale.US); System.out.println("United States: " + df.format(date)); }
}
</source>
Locale.KOREA
<source lang="java">
/*
* Output:
Japan: 4/28/06 Korea: Apr 28, 2006 United Kingdom: 28 April 2006 United States: Friday, April 28, 2006
* */
import java.text.DateFormat; import java.util.Date; import java.util.Locale; public class MainClass {
public static void main(String args[]) { Date date = new Date(); DateFormat df; df = DateFormat.getDateInstance(DateFormat.SHORT, Locale.JAPAN); System.out.println("Japan: " + df.format(date)); df = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.KOREA); System.out.println("Korea: " + df.format(date)); df = DateFormat.getDateInstance(DateFormat.LONG, Locale.UK); System.out.println("United Kingdom: " + df.format(date)); df = DateFormat.getDateInstance(DateFormat.FULL, Locale.US); System.out.println("United States: " + df.format(date)); }
}
</source>
Locale: setDefault(Locale newLocale)
<source lang="java">
import java.util.Locale; public class Main {
public static void main(String[] argv) throws Exception { Locale.setDefault(Locale.FRENCH); }
}
</source>
Locale.TAIWAN
<source lang="java">
import java.util.Locale; public class Main {
public static void main(String args[]) { Locale currentLocale = Locale.getDefault(); Locale locales[] = { Locale.TAIWAN, Locale.KOREA, Locale.ITALY, Locale.CANADA, Locale.CANADA_FRENCH }; System.out.println("CURRENT LOCALE:"); describeLocale(currentLocale); System.out.println("OTHER LOCALES:"); for (int i = 0; i < locales.length; ++i) describeLocale(locales[i]); } static void describeLocale(Locale l) { System.out.println("Country: " + l.getDisplayCountry()); System.out.println("Language: " + l.getDisplayLanguage()); System.out.println(); }
}
</source>
Locale.UK
<source lang="java">
/*
* Output:
Japan: 4/28/06 Korea: Apr 28, 2006 United Kingdom: 28 April 2006 United States: Friday, April 28, 2006
* */
import java.text.DateFormat; import java.util.Date; import java.util.Locale; public class MainClass {
public static void main(String args[]) { Date date = new Date(); DateFormat df; df = DateFormat.getDateInstance(DateFormat.SHORT, Locale.JAPAN); System.out.println("Japan: " + df.format(date)); df = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.KOREA); System.out.println("Korea: " + df.format(date)); df = DateFormat.getDateInstance(DateFormat.LONG, Locale.UK); System.out.println("United Kingdom: " + df.format(date)); df = DateFormat.getDateInstance(DateFormat.FULL, Locale.US); System.out.println("United States: " + df.format(date)); }
}
</source>
Locale.US
<source lang="java">
/*
* Output:
Japan: 4/28/06 Korea: Apr 28, 2006 United Kingdom: 28 April 2006 United States: Friday, April 28, 2006
* */
import java.text.DateFormat; import java.util.Date; import java.util.Locale; public class MainClass {
public static void main(String args[]) { Date date = new Date(); DateFormat df; df = DateFormat.getDateInstance(DateFormat.SHORT, Locale.JAPAN); System.out.println("Japan: " + df.format(date)); df = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.KOREA); System.out.println("Korea: " + df.format(date)); df = DateFormat.getDateInstance(DateFormat.LONG, Locale.UK); System.out.println("United Kingdom: " + df.format(date)); df = DateFormat.getDateInstance(DateFormat.FULL, Locale.US); System.out.println("United States: " + df.format(date)); }
}
</source>
new Locale(String language, String country, String variant)
<source lang="java">
import java.util.Locale; public class MainClass {
public static void main(String args[]) throws Exception { Locale defaultLocale = Locale.getDefault(); System.out.println(defaultLocale.getDisplayVariant()); System.out.println(defaultLocale.getDisplayVariant(Locale.US)); System.out.println((new Locale("en", "US", "WIN_TX_Austin")).getDisplayVariant()); }
}
</source>