javaweb 國際化:DateFormat,NumberFormat,MessageFormat,ResourceBundle的使用
Javaweb 國際化
DateFormat:格式化日期的工具類,本身是一個抽象類;
NumberFormat:格式化 數(shù)字 到 數(shù)字字符串,或貨幣字符串的字符類;
MessageFormat: 可以格式化模式字符串,模式字符串: 帶占位符的字符串: "Date: {0}, Salary: {1}",可以通過 format 方法會模式字符串進行格式化
ResourceBundle:資源包類,在類路徑(src)下需要有對應(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. 在類路徑下需要有對應(yīng)的資源文件: baseName.properties. 其中 baseName 是基名. * 2. 可以使用 基名_語言代碼_國家代碼.properties 來添加不同國家或地區(qū)的資源文件. i18n_zh_CN.properties * 3. 要求所有基名相同的資源文件的 key 必須完全一致. * 4. 可以使用 native2ascii 命令來得到 漢字 對一個的 asc 碼. Eclipse 內(nèi)置了工具 * 5. 可以調(diào)用 ResourceBundle 的 getBundle(基名, Locale 實例) 獲取獲取 ResourceBundle 對象 * 6. 可以調(diào)用 ResourceBundle 的 getString(key) 來獲取資源文件的 value 字符串的值. * 7. 結(jié)合 DateFormat, NumberFormat, MessageFormat 即可實現(xiàn)國際化. * */ @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: 格式化數(shù)字到數(shù)字字符串, 或貨幣字符串的工具類 * 1. 通過工廠方法獲取 NumberFormat 對象 * NumberFormat.getNumberInstance(locale); //僅格式化為數(shù)字的字符串 * 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. 先創(chuàng)建 DateFormat 對象: 創(chuàng)建 DateFormat 的子類 SimpleDateFormat 對象 * SimpleDateFormat(String pattern). * 其中 pattern 為日期, 時間的格式, 例如: yyyy-MM-dd hh:mm:ss * II. 調(diào)用 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 對象轉(zhuǎn)為一個字符串, 則可以通過 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 則為代表國家地區(qū)的 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 中表示國家或地區(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()); } }
以上就是對Java web國際化的資料整理,后續(xù)繼續(xù)補充相關(guān)資料,謝謝大家對本站的支持!
相關(guān)文章
解決Required request body is missing錯誤的問題
這篇文章主要介紹了解決Required request body is missing錯誤的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06解決spring AOP中自身方法調(diào)用無法應(yīng)用代理的問題
這篇文章主要介紹了解決spring AOP中自身方法調(diào)用無法應(yīng)用代理的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08Mybatis注解方式完成輸入?yún)?shù)為list的SQL語句拼接方式
這篇文章主要介紹了Mybatis注解方式完成輸入?yún)?shù)為list的SQL語句拼接方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11IDEA快速搭建spring?boot項目教程(Spring?initializr)
這篇文章主要介紹了IDEA快速搭建spring?boot項目教程(Spring?initializr),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01關(guān)于java.math.BigDecimal比較大小問題
這篇文章主要介紹了關(guān)于java.math.BigDecimal比較大小問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07