国际化

本文介绍了Java中实现文本、日期时间和数字的国际化方法及格式化技巧,包括使用ResourceBundle进行多语言支持、DateFormat处理日期时间格式以及NumberFormat处理数字和货币格式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、固定文本的国际化:
一个消息资源包,由多个properties文件组成的(基名和扩展名一致的)。
这些文件有着以下特点:
基名语言代码(ISO)国家区域代码(ISO).properties

message_zh_CN.properties
message_en_US.properties
message.properties(默认的)
 ResourceBundle
 Locale

二、日期时间的格式化:
 DateFormat: SimpleDateFormat

用户输入:String————->java.util.Date
Date parse(String)

显示数据:java.util.Date————->String
String format(Date)

三、数字的格式化(货币):
NumberFormat:

用户输入:String————->java.lang.Number
Number parse(String)

显示数据:java.util.Number————->String
String format(Number)

例子1:固定文本的国际化
1。TextI18n.java文件:

package com.itheima.i18n;

import java.util.Locale;
import java.util.ResourceBundle;

import org.junit.Test;

public class TextI18n {
    //按照本地区域信息读
    @Test
    public void test1(){
        ResourceBundle rb = ResourceBundle.getBundle("message");
        String s = rb.getString("hello");
        System.out.println(s);
    }
    //指定区域信息:Locale
    @Test
    public void test2(){
        Locale locale = Locale.US;
        ResourceBundle rb = ResourceBundle.getBundle("message",locale);
        String s = rb.getString("hello");
        System.out.println(s);
    }
}

2。message_en_US.properties文件:

#jdk\bin\native2ascii
hello=good morning
jsp.login.title=User Login
jsp.login.username=Username
jsp.login.password=Password
jsp.login.submit=Login

3。message_zh_CN.properties文件:

#jdk\bin\native2ascii
hello=\u60A8\u5403\u4E86\u5417
jsp.login.title=\u7528\u6237\u767B\u5F55
jsp.login.username=\u7528\u6237\u540D
jsp.login.password=\u5BC6\u7801
jsp.login.submit=\u767B\u5F55

4。通过JUnit调试结果:

您吃了吗
good morning

例子2:日起时间的格式化
DateFormatDemo.java文件:

package com.itheima.i18n;

import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.Locale;

import org.junit.Test;

public class DateFormatDemo {

    //2014830日 星期六 (日期部分)

    //下午043554秒 CST(时间部分)
    @Test
    public void test1(){

        Locale locale = Locale.CHINA;

        Date now = new Date();
        // FULL LONG MEDIUM(DEFAULT) SHORT
        DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL,locale);
        String str = df.format(now);
        System.out.println(str);
    }

    //yyyy-MM-dd
    @Test
    public void test2() throws ParseException{
        String s = "2014年8月30日 星期六 下午04时35分54秒 CST";
        Locale locale = Locale.CHINA;
        DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL,locale);
        Date d = df.parse(s);
        System.out.println(d);
    }
}

通过JUnit调试结果:

2016年9月26日 星期一 下午09时24分58秒 CST
Sat Aug 30 16:35:54 CST 2014

3。数字的格式化:
NumberFormatDemo.java文件:

package com.itheima.i18n;

import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Locale;

import org.junit.Test;

public class NumberFormatDemo {
    @Test
    public void test1(){
        int money = 100000000;
        NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);
        System.out.println(nf.format(money));
    }

    @Test
    public void test2() throws ParseException{
        String s = "$100,000,000.00";
        NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);
        Number n = nf.parse(s);
        System.out.println(n);
    }
}

通过JUnit调试的结果:

$100,000,000.00
100000000

例子4:数字的格式化(货币):
MessageFormatDemo.java文件:

package com.itheima.i18n;

import java.text.DateFormat;
import java.text.MessageFormat;
import java.text.NumberFormat;
import java.util.Date;
import java.util.Locale;

public class MessageFormatDemo {

    public static void main(String[] args) {
//      String pattern = "At 12:30 pm on jul 3,1998, a hurricance destroyed 99 houses and caused $1000000 of damage";
//      
//      Date time = new Date();//日期和时间
//      int num = 99;
//      int money = 100000;
//      
//      Locale locale = Locale.CHINA;
//      DateFormat df1 = DateFormat.getTimeInstance(DateFormat.DEFAULT, locale);
//      DateFormat df2 = DateFormat.getDateInstance(DateFormat.DEFAULT, locale);
//      NumberFormat nf = NumberFormat.getCurrencyInstance(locale);
//      
//      String stime = df1.format(time);
//      String sdate = df2.format(time);
//      String smoney = nf.format(money);
//      
//      System.out.println("At "+stime+" on "+sdate+", a hurricance destroyed "+num+" houses and caused "+smoney+" of damage");

        //占位符对应着参数的索引
        String pattern = "At {0,time,medium} on {1,date,medium}, a hurricance destroyed {2} houses and caused {3,number,currency} of damage";

        MessageFormat mf = new MessageFormat(pattern,Locale.US);
        String result = mf.format(new Object[]{new Date(),new Date(),99,1000000});
        System.out.println(result);

    }

}

运行结果:

At 9:31:25 PM on Sep 26, 2016, a hurricance destroyed 99 houses and caused $1,000,000.00 of damage
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值