10.1 Locale
Locale可以描述语言(ISO-639语言编码)、地点(ISO-3166国家编码)、变量;构造Locale类Locale germany = new Locale(“de”,”DE”[,”B”]),其中参数也可以用SDK定义的Locale对象常量取代.
常用方法:
Locale.getDefault(); 取得本地操作系统默认的locale
Locale.getDisplayName(); 返回用于描述locale的字符串
Locale.getLanguage(); 返回语言代码
Locale.getCountry(); 返回国家代码
Locale.getVariant(); 返回变体的字符串
10.2 数字与货币
工厂类NumberFormat有3个静态方法getNumberInstance()、getCurrencyInstance()和getPercentInstance(),能够对数字、货币量和百分比进行格式化和分析,如下:
NumberFormat nFmt = NumberFormat.getCurrencyInstance(locale)/ getCurrencyInstance ();
nFmt.format(123456.789);
分析数字:
nFmt.parse(String s); 分析给定字符串返回数字值,浮点返回Double,否则返回Long
10.3 日期与时间
工厂类DateFormat有3个静态方法getDateInstance(dateStyle,loc)、getTimeInstance(timeStyle,loc)和getDateTimeInstance(dateStyle,timeStyle,loc),能够对日期、时间和日期时间进行格式化和分析,如下:
DateFormat dFmt = DateFormat.getDateInstance(dateStyle,locale);
dFmt.format(new Date());
其中,style有DateFormat.DEFAULT、DateFormat.FULL、DateFormat.LONG、DateFormat.MEDIUM、DateFormat.SHORT
分析日期与时间:
dFmt.parse(String s); 分析给定的字符串并返回其中描述的日期/时间
dFmt.getCalendar(); 获取日历对象,用于从Date对象中提取年、月、日、时、分和秒
10.4 文本
排序 a.compareTo(b)
文本边界 BreakIterator(断开迭代器)
信息的格式化 MessageFormat
选择格式 ChoiceFormat
字符集转换
本地化问题和源文件
源文件 本地编码
类文件 UTF编码
虚拟机 Unicode编码
native2ascii 可将本地字符编码转换为无格式的ASCII,如:
native2ascii My.java Myfile.temp
native2ascii –reverse My.java Myfile.temp 逆向转换
native2ascii –encoding Cp437 My.java Myfile.temp 设定另一种编码,转换
10.5 资源包
查找资源步骤:
1. 设定资源文件
文件命名: ProgramResources_language_country/ ProgramResources_language
2. 加载资源文件
ResourceBundle cR = ResourceBundle.getBundle(“ProgramResources”,locale);
说明:它将依次加载一下名称资源文件
ProgramResources_language_country_variant
ProgramResources_language_country
ProgramResources_language
ProgramResources
将资源放入包中
resources.getString(“name”); 查找字符串资源
resources.getObject(“name”); 检索任意的对象
2007-08-09