Java使用Locale 对象的getDefault()方法和和setDefault()方法获取本地国家代码设置,在代码中定义了GetSetLocale 类。先来说说为什么要国际化,国际化的程序可以运行在不同操作系统上,在确定要以本地方法实现某种功能之前需要确定本地对象Locale,或者要以某种特定地区代码显示一段文本,就需要把缺省地区设置成为给定的地区代码。本例的具体实现方法:
1.GetSetLocale 类仅包括min()方法,在该方法中得到并且打印出缺省本地国家代码设置,根据命令行参数设置本地缺省国家代码,根据设定的缺省国家代码设置日期和数字显示的格式。
2.编写GetSetLocale 类,代码如下:
代码如下 |
复制代码 |
import java.text.*;
import java.util.*;
public class GetSetLocale {
public static void main(String[] args) {
// 得到并且打印出缺省本地国家代码设置。
Locale l = Locale.getDefault();
System.out.println("Today's Locale is " + l);
// 根据命令行参数设置本地缺省国家代码
switch (args.length) {
case 0:
Locale.setDefault(Locale.TRADITIONAL_CHINESE );
break;
case 1:
throw new IllegalArgumentException();
case 2:
Locale.setDefault(new Locale(args[0], args[1]));
break;
default:
System.out.println("Usage: SetLocale [language [country]]");
}
// 根据设定的缺省国家代码设置日期和数字显示的格式
DateFormat df = DateFormat.getInstance();
NumberFormat nf = NumberFormat.getInstance();
System.out.println("Locale set to " +
Locale.getDefault().getDisplayCountry() +
Locale.getDefault().getDisplayLanguage());
System.out.println(df.format(new Date()));
System.out.println(nf.format(123.4567));
}
}
|