学习总结之 时间数字格式化

一Date Format

1.1 SimpleDateFormat

        // hh小写为12小时制;注意大小写有影响的!!

        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        String date = sd.format(new Date());

        /** 2014-06-02 09:52:59 **/

 


 




1.2 DateFormat

    /**

     * // 日期时间格式

        DateFormat.FULL;

        DateFormat.LONG;

        DateFormat.MEDIUM;

        DateFormat.SHORT;

       

        日期类型:

            FULL    2014年6月1日星期日

            LONG    2014年6月1日

            MEDIUM  2014-6-1

            SHORT   14-6-1

        时间

            FULL    上午095610 CST

            LONG    上午095627

            MEDIUM  9:56:42

            SHORT   上午9:56

     */

 

public void test1() throws Exception {

        String str = "2014-6-1 上午095627";

        // 确定是那种地区的时间显示

        DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL,

                DateFormat.MEDIUM, Locale.CHINA);

        // 转化为Date类型

        Date date = df.parse(str);

        System.out.println(date);

        // 把时间格式化

        String date1 = df.format(new Date());

        System.out.println(date1);

    }

 

 

 
































二 Number Format

2.1 Number/Currency/tPercent

    @Test

    public void test1() throws Exception {

        NumberFormat nf1 = NumberFormat.getNumberInstance(Locale.US);

        System.out.println(nf1.format(10000));

        /** 10,000 **/

 

        NumberFormat nf2 = NumberFormat.getCurrencyInstance(Locale.US);

        System.out.println(nf2.format(10000));

        /** $10,000.00 **/

 

        NumberFormat nf3 = NumberFormat.getPercentInstance(Locale.US);

        System.out.println(nf3.format(0.2));

        /** 20% **/

    }

 

2.2 Example

//1.3 国际化多个数据

    public void format_message() throws ParseException {

        // 格式化: 日期,中文;     金额,显示中文

        String str = "on {0}, a hurricance destroyed 99 houses and caused {1} of damage";

        String s1 = "Jul 3, 1998 12:30 PM";

        String s2 = "$1000000";

       

        //s1 转换Date

        DateFormat df = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.SHORT, Locale.US);

        Date s1_date = df.parse(s1);

       

        //s2转换为数字

        NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);

        Number s2_num = nf.parse(s2);

       

        // 国际化信息(多个内容)

        MessageFormat mf = new MessageFormat(str);

       

        // 占位符对应的值

        Object[] obj = {

                DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.FULL, Locale.CHINA).format(s1_date),

                NumberFormat.getCurrencyInstance(Locale.CHINA).format(s2_num)

        };

       

        // 对多个内容进行国际化

        String result_str = mf.format(obj);

        // 国际化的字符串

        System.out.println(result_str);

    }

 

三 国际化

3.1 ResourceBundle

    <%

        // 获取语言环境对象

        Locale locale = request.getLocale();

        // 加载资源包

        ResourceBundle bunble = ResourceBundle.getBundle("msg",locale);

    %>

    <title><%=bunble.getString("title")%></title>

 

3.2 JSTL国际化


    <!-- 1. 设置语言环境 -->

    <fmt:setLocalevalue="${pageContext.request.locale}"/>

    <!-- 2. 加载资源包basename,属性文件文件名,需要带上包名 -->

    <fmt:setBundlebasename="cn/itcast/fmt/msg"var="b"/>

   

    <title><fmt:messagebundle="${b}" key="title"></fmt:message></title>

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值