javaweb 國(guó)際化:DateFormat,NumberFormat,MessageFormat,ResourceBundle的使用
Javaweb 國(guó)際化
DateFormat:格式化日期的工具類,本身是一個(gè)抽象類;
NumberFormat:格式化 數(shù)字 到 數(shù)字字符串,或貨幣字符串的字符類;
MessageFormat: 可以格式化模式字符串,模式字符串: 帶占位符的字符串: "Date: {0}, Salary: {1}",可以通過 format 方法會(huì)模式字符串進(jìn)行格式化
ResourceBundle:資源包類,在類路徑(src)下需要有對(duì)應(yīng)的資源文件: baseName.properties. 其中 baseName 是基名;
文件名為:test_zh_CN.properties,文件為:date=\u65E5\u671F,salary=\u5DE5\u8D44
文件名為:test_en_US.properties,文件為:date=date,salary=salary
import java.text.DateFormat;
import java.text.MessageFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.ResourceBundle;
import org.junit.Test;
public class I18nTest {
/**
* ResourceBundle: 資源包類.
*
* 1. 在類路徑下需要有對(duì)應(yīng)的資源文件: baseName.properties. 其中 baseName 是基名.
* 2. 可以使用 基名_語言代碼_國(guó)家代碼.properties 來添加不同國(guó)家或地區(qū)的資源文件. i18n_zh_CN.properties
* 3. 要求所有基名相同的資源文件的 key 必須完全一致.
* 4. 可以使用 native2ascii 命令來得到 漢字 對(duì)一個(gè)的 asc 碼. Eclipse 內(nèi)置了工具
* 5. 可以調(diào)用 ResourceBundle 的 getBundle(基名, Locale 實(shí)例) 獲取獲取 ResourceBundle 對(duì)象
* 6. 可以調(diào)用 ResourceBundle 的 getString(key) 來獲取資源文件的 value 字符串的值.
* 7. 結(jié)合 DateFormat, NumberFormat, MessageFormat 即可實(shí)現(xiàn)國(guó)際化.
*
*/
@Test
public void testResourceBundle(){
Locale locale = Locale.CHINA;
ResourceBundle resourceBundle = ResourceBundle.getBundle("test", locale);
System.out.println(resourceBundle.getString("date"));
System.out.println(resourceBundle.getString("salary"));
String dateLabel = resourceBundle.getString("date");
String salLabel = resourceBundle.getString("salary");
String str = "{0}:{1}, {2}:{3}";
Date date = new Date();
double sal = 12345.12;
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, locale);
String dateStr = dateFormat.format(date);
NumberFormat numberFormat = NumberFormat.getCurrencyInstance(locale);
String salStr = numberFormat.format(sal);
String result = MessageFormat.format(str, dateLabel, dateStr, salLabel, salStr);
System.out.println(result);
}
/**
* MessageFormat: 可以格式化模式字符串
* 模式字符串: 帶占位符的字符串: "Date: {0}, Salary: {1}"
* 可以通過 format 方法會(huì)模式字符串進(jìn)行格式化
*/
@Test
public void testMessageFormat(){
String str = "Date: {0}, Salary: {1}";
Locale locale = Locale.CHINA;
Date date = new Date();
double sal = 12345.12;
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, locale);
String dateStr = dateFormat.format(date);
NumberFormat numberFormat = NumberFormat.getCurrencyInstance(locale);
String salStr = numberFormat.format(sal);
String result = MessageFormat.format(str, dateStr, salStr);
System.out.println(result);
}
/**
* NumberFormat: 格式化數(shù)字到數(shù)字字符串, 或貨幣字符串的工具類
* 1. 通過工廠方法獲取 NumberFormat 對(duì)象
* NumberFormat.getNumberInstance(locale); //僅格式化為數(shù)字的字符串
* NumberFormat.getCurrencyInstance(locale); //格式為貨幣的字符串
*
* 2. 通過 format 方法來進(jìn)行格式化
* 3. 通過 parse 方法把一個(gè)字符串解析為一個(gè) Number 類型.
*/
@Test
public void testNumberFormat() throws ParseException{
double d = 123456789.123d;
Locale locale = Locale.FRANCE;
//
NumberFormat numberFormat = NumberFormat.getNumberInstance(locale);
String str = numberFormat.format(d);
System.out.println(str);
NumberFormat numberFormat2 = NumberFormat.getCurrencyInstance(locale);
str = numberFormat2.format(d);
System.out.println(str);
str = "123 456 789,123";
d = (Double) numberFormat.parse(str);
System.out.println(d);
str = "123 456 789,12 €";
d = (Double) numberFormat2.parse(str);
System.out.println(d);
}
/*
* 7. 若有一個(gè)字符串, 如何解析為一個(gè) Date 對(duì)象呢 ?
* I. 先創(chuàng)建 DateFormat 對(duì)象: 創(chuàng)建 DateFormat 的子類 SimpleDateFormat 對(duì)象
* SimpleDateFormat(String pattern).
* 其中 pattern 為日期, 時(shí)間的格式, 例如: yyyy-MM-dd hh:mm:ss
* II. 調(diào)用 DateFormat 的 parse 方法來解析字符串到 Date 對(duì)象.
*/
@Test
public void testDateFormat2() throws ParseException{
String str = "1990-12-12 12:12:12";
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date date = dateFormat.parse(str);
System.out.println(date);
}
/**
* DateFormat: 格式化日期的工具類.
* DateFormate 本身是一個(gè)抽象類.
*
* 1. 若只希望通過 DateFormat 把一個(gè) Date 對(duì)象轉(zhuǎn)為一個(gè)字符串, 則可以通過 DateFormat 的工廠方法來獲取 DateFormat 對(duì)象
* 2. 可以獲取只格式化 Date 的 DateFormat 對(duì)象: getDateInstance(int style, Locale aLocale)
* 3. 可以獲取只格式化 Time 的 DateFormat 對(duì)象: getTimeInstance(int style, Locale aLocale)
* 4. 可以獲取既格式化 Date, 也格式化 Time 的 DateFormat 對(duì)象:
* getDateTimeInstance(int dateStyle, int timeStyle, Locale aLocale)
* 5. 其中 style 可以取值為: DateFormat 的常量: SHORT, MEDIUM, LONG, FULL. Locale 則為代表國(guó)家地區(qū)的 Locale 對(duì)象
* 6. 通過 DateFormat 的 format 方法來格式化個(gè) Date 對(duì)象到字符串.
*/
@Test
public void testDateFormat(){
Locale locale = Locale.US;
Date date = new Date();
System.out.println(date);
//獲取 DateFormat 對(duì)象
DateFormat dateFormat =
DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.MEDIUM, locale);
String str = dateFormat.format(date);
System.out.println(str);
}
/**
* Locale: Java 中表示國(guó)家或地區(qū)的類. JDK 中提供了很多常量.
* 也可以通過 Locale(languageCode, countryCode) 的方式來創(chuàng)建
* 在 WEB 應(yīng)用中可以通過 request.getLocale() 方法來獲取.
*/
@Test
public void testLocale(){
Locale locale = Locale.CHINA;
System.out.println(locale.getDisplayCountry());
System.out.println(locale.getLanguage());
locale = new Locale("en", "US");
System.out.println(locale.getDisplayCountry());
System.out.println(locale.getLanguage());
}
}
以上就是對(duì)Java web國(guó)際化的資料整理,后續(xù)繼續(xù)補(bǔ)充相關(guān)資料,謝謝大家對(duì)本站的支持!
相關(guān)文章
解決Required request body is missing錯(cuò)誤的問題
這篇文章主要介紹了解決Required request body is missing錯(cuò)誤的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
解決spring AOP中自身方法調(diào)用無法應(yīng)用代理的問題
這篇文章主要介紹了解決spring AOP中自身方法調(diào)用無法應(yīng)用代理的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
Java如何對(duì)方法進(jìn)行調(diào)用詳解
今天給大家整理了Java如何對(duì)方法進(jìn)行調(diào)用,文中有非常詳細(xì)的介紹及代碼示例,對(duì)正在學(xué)習(xí)java的小伙伴們很有幫助,需要的朋友可以參考下2021-06-06
Mybatis注解方式完成輸入?yún)?shù)為list的SQL語句拼接方式
這篇文章主要介紹了Mybatis注解方式完成輸入?yún)?shù)為list的SQL語句拼接方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
IDEA快速搭建spring?boot項(xiàng)目教程(Spring?initializr)
這篇文章主要介紹了IDEA快速搭建spring?boot項(xiàng)目教程(Spring?initializr),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01
關(guān)于java.math.BigDecimal比較大小問題
這篇文章主要介紹了關(guān)于java.math.BigDecimal比較大小問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07

