学习SimpledateFormat

来源:互联网 发布:mac mounty 灰色文件 编辑:程序博客网 时间:2024/06/06 01:23

要实现中文格式化,其实很简单,就是使用如下这个构造函数
SimpleDateFormat(String pattern, Locale locale)
例如

SimpleDateFormat matter2=new SimpleDateFormat("xxxxx", Locale.CHINESE);

也可以把Locale.CHINESE换成Locale.CHINA,我检查过,效果是一样的,
这样做的好处是,在你调用format方法的时候,他会自动把例如月份转换成中文格式
例如

       SimpleDateFormat matter2=new SimpleDateFormat("北京时间 yyyy年 MMM dd日 HH:mm:ss(a)(EE) ", Locale.CHINESE);    System.out.println("现在的时间: "+matter2.format(new Date()));

输出

现在的时间: 北京时间 2014年 十二月 15日 23:28:59(下午)(星期一) 

在输入了3个MMM***后(记住,不能少),月份会被自动转成中文的,除此之外还有aEE**,
至于为什么要输入3个MMM才有效,文档里给出如下解释

Month: If the number of pattern letters is 3 or more, the month is interpreted as text; otherwise, it is interpreted as a number.

简单地说就是当MMM的个数在3个以上的时候,这个模式才会被解释为文本,否则是数字

0 0
原创粉丝点击