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

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

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國際化的資料整理,后續繼續補充相關資料,謝謝大家對本站的支持!

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日本黄视频在线播放 | 晓雪老师我要进你里面好爽 | 亚洲AV久久无码精品九号软件 | 国产精品久久久久久久久 | 四虎影院久久久 | 99热资源 | 999热在线精品观看全部 | 高贵女王调奴vk | 关晓彤被调教出奶水的视频 | 亚洲激情网 | 久久综合给会久久狠狠狠 | 色多多在线观看视频 | 久久水蜜桃亚洲AV无码精品偷窥 | 欧美综合国产精品日韩一 | 国产99区 | 免费国产高清视频 | 国产成人无精品久久久 | 日韩精品在线视频观看 | 亚洲精品人成网在线播放影院 | 日本videosdesexo乱 | 美女被视频 | 国产高清不卡视频在线播放 | 女教师系列三上悠亚在线观看 | 我把校花黑色蕾丝胸罩脱了 | 四虎永久在线精品波多野结衣 | 91国内精品久久久久怡红院 | 香蕉精品国产高清自在自线 | 国产高清自拍 | 第一福利在线视频 | 好男人在线观看免费高清2019韩剧 | 国内精品久久久久影院男同志 | 三级全黄的视频 | 久久偷拍免费2017 | 9久热这里只有精品视频在线观看 | 毛片免费视频观看 | 亚洲精品久久啪啪网站成年 | 日韩中文字幕一区 | 欧美一区二区三区不卡视频 | 精品日韩欧美一区二区三区 | 出差被灌醉绝伦的上司日本 | 久久九九亚洲精品 |