Board logo

标题: Unicode 及编码方式概述-5 [打印本页]

作者: look_w    时间: 2018-3-17 13:06     标题: Unicode 及编码方式概述-5

根据对比结果可以看出,Java 和 ICU4J 对时间日期格式化的输出结果显示有一些不同,日期和时间格式取决于语言和文化偏好,有些用户可能更喜欢                Java 的格式显示,其他用户可能更喜欢 ICU                的格式显示。这些差异不会在显示输出结果时造成问题。但是格式化差异在解析时间日期字符串时可能会出现一些问题,ICU LONG FORMAT                日期和时间解析功能不能分析基于 Java 的 LONG FORMAT 日期。由于 ICU 解析器不是基于 Java 的日期和时间格式的规则,所以                ICU 解析器将拒绝分析基于 Java 格式的字符串,相反,Java 解析函数将无法分析基于 ICU                的格式。在解析和显示格式化时间日期的时候,需要注意几点:
当我们需要创建日历组件的时候,可以使用 Java 库中的日历对象(java.util.Calendar),也可以使用 ICU4J                库中的日历,但是对于支持多语言的程序来说,最好使用 ICU4J                库中的日历空间,因为它支持的类型更多,对国际化支持更好,除了公历之外,还有佛教日历,中国的农历,台湾的民国日历,日本日历等等。ICU                还支持公历日历和其他非公历日历之间的转换,如清单 2 所示的代码片段。
清单 2. ICU4J                对非公历日历支持               
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
public ULocale setLocale(ActionEvent ev) {
    int type = lcList.getSelectedIndex();
    ULocale lc = null;
    if (type == 0) {
        lc = new ULocale("ja_JP@calendar=japanese");
    } else if (type == 1) {
    lc = new ULocale("th_TH@calendar=buddhist");
    } else if (type == 2) {
    lc = new ULocale("ar_SA@calendar=islamic");
    } else if (type == 3) {
    lc = new ULocale("he_IL@calendar=hebrew");
    } else if (type == 4) {
    lc = new ULocale("hi_IN@calendar=indian");
    } else if (type == 5) {
    lc = new ULocale("zh_TW@calendar=roc");
    } else {
    lc = new ULocale("en_US@calendar=coptic");
    }
    return lc;
}
private Calendar setCalendar(ULocale lc, Date date) {
    int type = lcList.getSelectedIndex();
    Calendar ngCalendar;
    if (type == 0) {
        ngCalendar = JapaneseCalendar.getInstance(lc);
    } else if (type == 1) {
    ngCalendar = BuddhistCalendar.getInstance(lc);
    } else if (type == 2) {
    ngCalendar = IslamicCalendar.getInstance(lc);
    } else if (type == 3) {
    ngCalendar = HebrewCalendar.getInstance(lc);
    } else if (type == 4) {
    ngCalendar = IndianCalendar.getInstance(lc);
    } else if (type == 5) {
    ngCalendar = TaiwanCalendar.getInstance(lc);
    } else if (type == 6) {
    ngCalendar = CopticCalendar.getInstance(lc);
    } else {
    ngCalendar = GregorianCalendar.getInstance(lc);
    }
    ngCalendar.setTime(date);
    return ngCalendar;
}




图 10. ICU4J                    对公历和非公历日历之间的转换




欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) Powered by Discuz! 7.0.0