一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - javaweb 國際化:DateFormat,NumberFormat,MessageFormat,ResourceBundle的使用

javaweb 國際化:DateFormat,NumberFormat,MessageFormat,ResourceBundle的使用

2020-06-11 16:19lqh JAVA教程

本文主要介紹javaWEB國際化的知識,這里整理了詳細的資料及實現代碼,有興趣的小伙伴可以參考下

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
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國際化的資料整理,后續繼續補充相關資料,謝謝大家對本站的支持!

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日本乱中文字幕系列在线观看 | 九九精品视频一区二区三区 | 亚洲黄视频在线观看 | 欧美一级特黄特色大片 | 亚洲日本va午夜中文字幕 | 91热爆| 艾秋麻豆果冻剧传媒在线播放 | 国产精品成人免费福利 | 嫩草影院永久在线一二三四 | 91热国内精品永久免费观看 | 免费看国产一级特黄aa大片 | 爱情岛论坛亚洲永久入口口 | 精品视频入口 | 99国产小视频 | 天美网站传媒入口网址 | 国精品午夜dy8888狼人 | 欧美成人福利 | 国产亚洲精品aaa大片 | 亚洲偷窥图区色 | 2015台湾永久免费平台 | 成人小视频在线观看免费 | se在线播放| 我和么公的秘密小说免费 | 99精品视频在线观看免费 | 日韩成人在线视频 | gay台湾无套男同志可播放 | 视频免费观看在线播放高清 | 免费看1级伦理 | 亚欧有色在线观看免费版高清 | 91麻豆精品激情在线观看最新 | 肉搏潘金莲三级18春 | 亚洲 无码 制服 日韩 | 久久伊人精品青青草原2021 | 国产午夜视频在线观看网站 | 513热点网深夜影院影院诶 | 国产欧美日韩在线不卡第一页 | 亚洲精品综合一二三区在线 | 成人精品第一区二区三区 | 亚洲 欧美 制服 校园 动漫 | 边吃奶边扎下面 | 日本成熟bbxxxxxxxx |