国际化
全球化的Internet需要全球化的软件,这就代表同一版本的软件要适用于不同的地区市场。软件的全球化首先是要使程序能支持多国语音,即国际化。而由于java语言具有平台无关,可移植性好等特点,并且提供了强大的类库,java语言采用Unicode编码,所以可以使用java语言实现国际化。
Locale类
Locale实例可以代表一种特定的语言和地区。
//创建中国大陆的Locale对象:
Locale locale=new Locale("zh","CN");//zh为汉语的编码,CN为中国的编码
我们可以通过Locale类的方法获取JAVA支持的国家和语言
ResourceBundle类
ResourceBundle类用于加载国家和语言资源包。
资源文件的命名形式:
baseName_language_country.properties
baseName_language.properties
baseName.properties
资源文件的内容是"key-value"对组成.
//读取默认资源文件
ResourceBundle resb=new ResourceBundle.getBundle("myres",Locale.getDefault());
格式化处理
数字格式化
数字表示方式是根据不同的Locale进行格式化的。
在java中是通过java.text包中提供的NumberFormat类实现对于数字,百分比的格式化以及对字符串对象的解析。
货币格式化
与数字格式化一样,都是通过NumberFormat类中的方法进行格式化。
//NumberFormat对象:
NumberFormat cnNf=new NumberFormat.getCurrencyInstance(cnLocale);
日期格式化
java中日期的格式化通过DateFormat类完成。
public class DateFormatDemo{
public static void print(Date date,Locale locale){
DateFormat df1=DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormt.FULL,locale);
DateFormat df2=DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormt.LONG,locale);
DateFormat df3=DateFormat.getDateTimeInstance(DateFormat.DEFAULT,DateFormt.DEFAULT,locale);
DateFormat df4=DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormt.SHORT,locale);
System.out.println(df1.format(date));
System.out.println(df2.format(date));
System.out.println(df3.format(date));
System.out.println(df4.format(date));
}
public static void main(String[]args){
Date now=new Date();
Locale cnLocale=new Locale("zh","CN");
Locale usLocale=new Locale("en","US");
System.out.println("中文格式:");
print(now,cnLocale);
System.out.println("英文格式:");
print(now,usLocale);
}
}
在java中还有一个SimpleDateFormat类,该类是DateFormat类的子类,比起DateFormat更加简便。
SimpleDateFormat类可以通过预定义的模式字符够着特定的模式串,以此来完成日期的格式串。
在java8中,新增了DateTimeFoemat类,该类不仅可以将时间日期格式化成字符串,还可以将特定的字符串解析成时间日期对象。
总结
随着全球化的发展,对于应用程序的全球化也更加的看重,应用程序的全球化也使各国之间的技术交流更加的便捷。