Javaweb 國際化
DateFormat:格式化日期的工具類,本身是一個抽像類;
NumberFormat:格式化數字到數字字符串,或貨幣字符串的字符類;
MessageFormat: 可以格式化模式字符串,模式字符串: 帶佔位符的字符串: "Date: {0}, Salary: {1}",可以通過format 方法會模式字符串進行格式化
ResourceBundle:資源包類,在類路徑(src)下需要有對應的資源文件: 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. 在類路徑下需要有對應的資源文件: baseName.properties. 其中baseName 是基名. * 2. 可以使用基名_語言代碼_國家代碼.properties 來添加不同國家或地區的資源文件. i18n_zh_CN.properties * 3. 要求所有基名相同的資源文件的key 必須完全一致. * 4. 可以使用native2ascii 命令來得到漢字對一個的asc 碼. Eclipse 內置了工具* 5. 可以調用ResourceBundle 的getBundle(基名, Locale 實例) 獲取獲取ResourceBundle 對象* 6. 可以調用ResourceBundle 的getString(key) 來獲取資源文件的value 字符串的值. * 7. 結合DateFormat, NumberFormat, MessageFormat 即可實現國際化. * */ @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 方法會模式字符串進行格式化*/ @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: 格式化數字到數字字符串, 或貨幣字符串的工具類* 1. 通過工廠方法獲取NumberFormat 對象* NumberFormat.getNumberInstance(locale); //僅格式化為數字的字符串* NumberFormat.getCurrencyInstance(locale); //格式為貨幣的字符串* * 2. 通過format 方法來進行格式化* 3. 通過parse 方法把一個字符串解析為一個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. 若有一個字符串, 如何解析為一個Date 對象呢? * I. 先創建DateFormat 對象: 創建DateFormat 的子類SimpleDateFormat 對象* SimpleDateFormat(String pattern). * 其中pattern 為日期, 時間的格式, 例如: yyyy-MM-dd hh:mm:ss * II. 調用DateFormat 的parse 方法來解析字符串到Date 對象. */ @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 本身是一個抽像類. * * 1. 若只希望通過DateFormat 把一個Date 對象轉為一個字符串, 則可以通過DateFormat 的工廠方法來獲取DateFormat 對象* 2. 可以獲取只格式化Date 的DateFormat 對象: getDateInstance(int style, Locale aLocale) * 3. 可以獲取只格式化Time 的DateFormat 對象: getTimeInstance(int style, Locale aLocale) * 4. 可以獲取既格式化Date, 也格式化Time 的DateFormat 對象: * getDateTimeInstance(int dateStyle, int timeStyle, Locale aLocale) * 5. 其中style 可以取值為: DateFormat 的常量: SHORT, MEDIUM, LONG, FULL. Locale 則為代表國家地區的Locale 對象* 6. 通過DateFormat 的format 方法來格式化個Date 對像到字符串. */ @Test public void testDateFormat(){ Locale locale = Locale.US; Date date = new Date(); System.out.println(date); //獲取DateFormat 對象DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.MEDIUM, locale); String str = dateFormat.format(date); System.out.println(str); } /** * Locale: Java 中表示國家或地區的類. JDK 中提供了很多常量. * 也可以通過Locale(languageCode, countryCode) 的方式來創建* 在WEB 應用中可以通過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()); } }以上就是對Java web國際化的資料整理,後續繼續補充相關資料,謝謝大家對本站的支持!