1.TestI18n
import java.util.Locale;
public class TestI18n {
public static void main(String[] args) {
Locale locale=Locale.CHINA;
System.out.println(locale.getCountry());
System.out.println(locale.getLanguage());
System.out.println("-------------------");
locale=Locale.US;
System.out.println(locale.getCountry());
System.out.println(locale.getLanguage());
System.out.println("-------------------");
Locale l=new Locale("zh","CN");
System.out.println(l.getDisplayCountry());
}
}
2. TestDataFormat
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.Locale;
public class TestDataFormat {
public static void main(String[] args) throws Exception {
Date data=new Date();
DateFormat df=DateFormat.getDateInstance(DateFormat.LONG, Locale.CHINA);
//日期转化为字符串
String source=df.format(data);
System.out.println("***********************");
System.out.println("DateFormat.LONG= "+source);
System.out.println("***********************");
df=DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.CHINA);
//日期转化为字符串
source=df.format(data);
System.out.println("***********************");
System.out.println("DateFormat.MEDIUM= "+source);
System.out.println("***********************");
df=DateFormat.getDateInstance(DateFormat.SHORT, Locale.CHINA);
//日期转化为字符串
source=df.format(data);
System.out.println("***********************");
System.out.println("DateFormat.SHORT= "+source);
System.out.println("################################");
//转化为日期.当字符串的格式不知道的情况下,先将日期转化为字符串,找到对应的格式做改动
df=DateFormat.getDateInstance(DateFormat.SHORT, Locale.CHINA);
System.out.println(df.format(new Date()));//12-10-24
String dateStr="12-10-24";
df=DateFormat.getDateInstance(DateFormat.SHORT, Locale.CHINA);
Date d=df.parse(dateStr);
System.out.println("转化为日期: "+d);
}
}
3. TestNumberFormat
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Locale;
public class TestNumberFormat {
public static void main(String[] args) throws Exception {
Locale locale=Locale.CHINA;
NumberFormat nf=NumberFormat.getInstance(locale);
double num=123456789.78;
//格式规范
String src=nf.format(num);
System.out.println(src);
System.out.println("***********************");
locale=Locale.FRANCE;
nf=NumberFormat.getInstance(locale);
src=nf.format(num);
System.out.println(src);
System.out.println("***********************");
locale=Locale.CHINA;
nf=NumberFormat.getInstance(locale);
src="456,789.89";
//解析给定字符串开头的文本,生成一个数值
System.out.println(nf.parse(src));
}
}
4. TestMessageFormat
import java.text.MessageFormat;
public class TestMessageFormat {
public static void main(String[] args) {
//String pattern=" I worked at dhc and good ";
String pattern=" I {0} at {1} and {2} ";
MessageFormat mf=new MessageFormat(pattern);
//格式化一个对象数组,并将MessageFormat的模式添加到所提供的StringBuffer
Object[] params=new Object[]{"worked","dhc","good"};
StringBuffer buf=new StringBuffer();
mf.format(params, buf, null);
System.out.println(buf.toString());
}
}