Java国际化

本文通过具体示例介绍了如何在Java中实现文本、日期和数字的国际化,包括资源文件的使用及不同区域设置的显示效果。
package com.wnl.demo;

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

import org.junit.Test;

public class Demo01 {


    /**
     * 文本国际化
     */
    @Test
    public void test1(){
        ResourceBundle rb=ResourceBundle.getBundle("msg");
        String greet=rb.getString("hello");
        System.out.println(greet);
    }

    @Test
    public void test2(){
        Locale locale=Locale.US;//之地国家地区
        //如果文件里面没有locale指定的国家或者地区,则使用本地平台默认的语言
        ResourceBundle rb=ResourceBundle.getBundle("msg",locale);
        String greet=rb.getString("hello");
        System.out.println(greet);
    }
    /**
     * 日期国际化
     */
    @Test
    public void test3(){
        Locale locale=Locale.CHINA;
        Date date=new Date();
        /*getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, locale)
         * 第一个DateFormat.FULL:日期格式
         * 第二个DateFormat.FULL:时间格式
         * locale:区域
         */
        DateFormat df=DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, locale);
        String s1=df.format(date);
        System.out.println(s1);
        //使用默认
        DateFormat df2=DateFormat.getDateTimeInstance();
        String s2=df2.format(date);
        System.out.println(s2);
    }

    /**
     * 数字国际化
     */

    @Test
    public void test4(){
        int money=1000;
        Locale inLocale=Locale.CHINA;
        NumberFormat nf1=NumberFormat.getCurrencyInstance(inLocale);
        String s1=nf1.format(money);
        System.out.println(s1);
        //设置百分比
        NumberFormat nf2=NumberFormat.getPercentInstance(inLocale);
    //  nf2.setMinimumFractionDigits(4);//设置小数位数最小值   0.0300%
        nf2.setMaximumFractionDigits(6);//设置小数位数最大值
        float intrest=0.0003f;
        String s2=nf2.format(intrest);
        System.out.println(s2);
    }
    /**
     * 批量格式化
     */
    @Test
    public void test5(){
        Locale locale=Locale.CHINA;
        String s="在{0,time,short},英国剑桥大学拒绝了{1}个中国学生的雅思成绩凭证,导致{2}中国学生重新考试";
        Object obj[]=new Object[]{new Date(),1000,680};
        MessageFormat mf=new MessageFormat(s,locale);
        String s1=mf.format(obj);
        System.out.println(s1);
    }


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值