Java by API/java.util/Locale — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 14:10, 31 мая 2010

Locale.CANADA_FRENCH

 
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();
  }
}





Locale.GERMANY

 

/*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));
    
  }
}





Locale: getDefault()

 
/*
 * 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);
  }
}





Locale: getDisplayCountry()

 
/*
 * 
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));
      }
    }
  }
}





Locale: getDisplayCountry(Locale inLocale)

 
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));
  }
}





Locale: getDisplayLanguage()

 
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();
  }
}





Locale: getDisplayName()

 
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));
  }
}





Locale: getDisplayName(Locale inLocale)

 
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));
  }
}





Locale: getDisplayVariant()

 
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());
  }
}





Locale: getDisplayVariant(Locale inLocale)

 
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());
  }
}





Locale: getISOCountries()

 
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]);
  }
}





Locale.getISOLanguages()

 
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]);
  }
}





Locale.ITALY

 
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();
  }
}





Locale.JAPAN

 
/*
 * 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));
  }
}





Locale.KOREA

 
/*
 * 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));
  }
}





Locale: setDefault(Locale newLocale)

 
import java.util.Locale;
public class Main {
  public static void main(String[] argv) throws Exception {
    Locale.setDefault(Locale.FRENCH);
  }
}





Locale.TAIWAN

 

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();
  }
}





Locale.UK

 
/*
 * 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));
  }
}





Locale.US

 
/*
 * 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));
  }
}





new Locale(String language, String country, String variant)

 
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());
  }
}