Local和国际化
Local和国际化是什么?
不同国家的语言、文字、数字、日期都有不同显示,而Local可用根据系统语言对其进行显示
Local中的静态函数
获取当前的语言和国家
Locale defaultLocal = Locale.getDefault();
获取所有的语言及国家
Locale[] locales = Locale.getAvailableLocales();
获取所有国家
String[] countries = Locale.getISOCountries();
获取所有的语言
String[] languages = Locale.getISOLanguages();
获取某个国家对应的标签,如下为en-US
String s = Locale.US.toLanguageTag();
获取某个国家对应的语言,如下为en
String s = Locale.ENGLISH.toLanguageTag();
构建Local
使用标签生成Local
Locale locale = Locale.forLanguageTag("en-US");
通过构造函数生成Local
Locale locale=new Locale("en");
Locale locale=new Locale("en","US");
ISO2和ISO3转换
public static String getIOS3CountryByIOS2(String code) {
return new Locale("", code).getISO3Country();
}
public static String getISO2CountryByISO3(String code) {
String[] isoCountries = Locale.getISOCountries();
for (String isoCountry : isoCountries) {
if (new Locale("", isoCountry).getISO3Country().equals(code)) {
return isoCountry;
}
}
return "";
}
public static String getISO3LanguageByISO2(String code) {
return new Locale(code).getISO3Language();
}
/*public static String getISO2LanguageByISO3(String code) {
String[] isoLanguages = Locale.getISOLanguages();
for (String isoLanguage : isoLanguages) {
if (new Locale(isoLanguage).getLanguage().equals(code)) {
return isoLanguage;
}
}
return "";
}*/
显示名字
getDisplayName()输出Local的名字,如下为English (United States)、英语 (美国)
Locale locale=new Locale("en","US");
System.out.println(locale.getDisplayName());
System.out.println(locale.getDisplayName(Locale.CHINA));
数字格式化
如以中国数字的形式解析123456.78,则为123,456.78
NumberFormat numberInstance = NumberFormat.getNumberInstance(Locale.CHINA);
String format = numberInstance.format(123456.78);
System.out.println(format);
反解析123,456.78,Number类型不能转为基本数据类型,需调用doubleValue(),格式不正确抛出ParseException
String s = "123,456.78";
NumberFormat numberInstance = NumberFormat.getNumberInstance(Locale.CHINA);
Number parse = null;
try {
parse = numberInstance.parse(s.trim());
} catch (ParseException e) {
e.printStackTrace();
}
if (parse != null) {
double b = parse.doubleValue();
System.out.println(b);
}
货币格式化
如以中国货币的形式解析123456.78,则为¥123,456.78
NumberFormat currencyInstance = NumberFormat.getCurrencyInstance(Locale.CHINA);
String format = currencyInstance.format(123456.78);
System.out.println(format);
反解析¥123,456.78,Number类型不能转为基本数据类型,需调用doubleValue(),格式不正确抛出ParseException
String s = "¥123,456.78";
Number parse = null;
NumberFormat currencyInstance = NumberFormat.getCurrencyInstance(Locale.CHINA);
try {
parse = currencyInstance.parse(s.trim());
} catch (ParseException e) {
e.printStackTrace();
}
if (parse != null) {
double b = parse.doubleValue();
System.out.println(b);
}
若同时用两种货币结算,又不想创建两个NumberFormat实例,如在中国用美元支付则显示US$123,456.78
NumberFormat currencyInstance = NumberFormat.getCurrencyInstance(Locale.CHINA);
currencyInstance.setCurrency(Currency.getInstance(Locale.US));
String format = currencyInstance.format(123456.78);
System.out.println(format);
百分比格式化
如以中国百分比的形式解析0.2,则为20%
NumberFormat percentInstance = NumberFormat.getPercentInstance(Locale.CHINA);
String format = percentInstance.format(0.2);
System.out.println(format);
反解析20%,Number类型不能转为基本数据类型,需调用doubleValue(),格式不正确抛出ParseException
String s = "20%";
NumberFormat percentInstance = NumberFormat.getPercentInstance(Locale.CHINA);
Number parse = null;
try {
parse = percentInstance.parse(s);
} catch (ParseException e) {
e.printStackTrace();
}
if (parse != null) {
double b = parse.doubleValue();
System.out.println(b);
}
时间日期格式化
格式化时间日期需要考虑:
- 月份和星期的数字应该用本地语言
- 年月日的顺序符合本地习惯
- 公历可能并不是本地首选表示方法
- 需考虑本地的时区
使用DateTimeFormatter.ofLocalizedDate格式化LocalDate
如下输出:
- 2021-12-01
- 12/1/21
- Dec 1, 2021
- December 1, 2021
- Wednesday, December 1, 2021
LocalDate today = LocalDate.now();
System.out.println(today);
DateTimeFormatter shortDateFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT);
System.out.println(shortDateFormatter.format(today));
DateTimeFormatter mediumDateFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM);
System.out.println(mediumDateFormatter.format(today));
DateTimeFormatter longDateFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG);
System.out.println(longDateFormatter.format(today));
DateTimeFormatter fullDateFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
System.out.println(fullDateFormatter.format(today));
若要指定国家,则在后面添加withLocale(Local local)
DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT).withLocale(Locale.CHINA);
如上改为中国则输出
- 2021-12-01
- 2021/12/1
- 2021年12月1日
- 2021年12月1日
- 2021年12月1日星期三
使用DateTimeFormatter.ofLocalizedTime格式化LocalTime
如下输出(FormatStyle.LONG和FormatStyle.FULL只适用于ZonedDateTime)
- 09:06:01.582
- 9:06 AM
- 9:06:01 AM
LocalTime today = LocalTime.now();
System.out.println(today);
DateTimeFormatter shortTimeFormatter = DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT);
System.out.println(shortTimeFormatter.format(today));
DateTimeFormatter mediumTimeFormatter = DateTimeFormatter.ofLocalizedTime(FormatStyle.MEDIUM);
System.out.println(mediumTimeFormatter.format(today));
/*DateTimeFormatter longTimeFormatter = DateTimeFormatter.ofLocalizedTime(FormatStyle.LONG);
System.out.println(longTimeFormatter.format(today));
DateTimeFormatter fullTimeFormatter = DateTimeFormatter.ofLocalizedTime(FormatStyle.FULL);
System.out.println(fullTimeFormatter.format(today));*/
若要指定国家,则在后面添加withLocale(Local local)
DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT).withLocale(Locale.CHINA);
如上改为中国则输出
- 09:23:18.817
- 上午9:23
- 上午9:23:18
使用DateTimeFormatter.ofLocalizedDateTime格式化localDateTime
如下输出(FormatStyle.LONG和FormatStyle.FULL只适用于ZonedDateTime)
- 2021-12-01T09:11:34.939
- 12/1/21, 9:11 AM
- Dec 1, 2021, 9:11:34 AM
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDateTime);
DateTimeFormatter shortDateTimeFormatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);
System.out.println(shortDateTimeFormatter.format(localDateTime));
DateTimeFormatter mediumDateTimeFormatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
System.out.println(mediumDateTimeFormatter.format(localDateTime));
/*DateTimeFormatter longDateTimeFormatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG);
System.out.println(longDateTimeFormatter.format(localDateTime));
DateTimeFormatter fullDateTimeFormatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL);
System.out.println(fullDateTimeFormatter.format(localDateTime));*/
若要指定国家,则在后面添加withLocale(Local local)
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT).withLocale(Locale.CHINA);
如上改为中国则输出
- 2021-12-01T09:27:37.435
- 2021/12/1 上午9:27
- 2021年12月1日 上午9:27:37
使用DateTimeFormatter.ofLocalizedDateTime格式化ZonedDateTime
如下输出:
- 2021-12-01T09:15:36.639Z[GMT]
- 12/1/21, 9:15 AM
- Dec 1, 2021, 9:15:36 AM
- December 1, 2021 at 9:15:36 AM GMT
- Wednesday, December 1, 2021 at 9:15:36 AM Greenwich Mean Time
ZonedDateTime zonedDateTime = ZonedDateTime.now();
System.out.println(zonedDateTime);
DateTimeFormatter shortDateTimeFormatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);
System.out.println(shortDateTimeFormatter.format(zonedDateTime));
DateTimeFormatter mediumDateTimeFormatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
System.out.println(mediumDateTimeFormatter.format(zonedDateTime));
DateTimeFormatter longDateTimeFormatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG);
System.out.println(longDateTimeFormatter.format(zonedDateTime));
DateTimeFormatter fullDateTimeFormatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL);
System.out.println(fullDateTimeFormatter.format(zonedDateTime));
若要指定国家,则在后面添加withLocale(Local local)
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT).withLocale(Locale.CHINA);
如上改为中国则输出
- 2021-12-01T10:34:31.890Z[GMT]
- 2021/12/1 上午10:34
- 2021年12月1日 上午10:34:31
- 2021年12月1日 GMT+00:00 上午10:34:31
- 2021年12月1日星期三 格林尼治标准时间 上午10:34:31
排序格式化
对于String类型的比较,其compareTo方法使用的是UTF-16编码,如有些国家认为a和A不应该排一起,此时可调用Collator获取不同国家的字符串比较器
List<String> list = new ArrayList<>();
list.add("A");
list.add("a");
list.add("b");
Collator collator = Collator.getInstance(Locale.KOREA);
list.sort(collator);
System.out.println(list);