日期格式国际化

来源:互联网 发布:快压软件官方下载 编辑:程序博客网 时间:2024/05/21 22:46

/** 日期格式国际化:
* DataFormat类:
* 1.可以
*将一个对格式化为一个String指定对象
* format
* 2. 可以将String解析成为data对象
* parse
*/
日期格式国际化`public class DataFormatTest {
@Test
public void test01() {

    //只显示年月日    DateFormat dtf1 = DateFormat.getDateInstance();    //只显示时分秒    DateFormat dtf2 = DateFormat.getTimeInstance();    //显示年月日和时分秒    DateFormat dtf3 = DateFormat.getDateTimeInstance();    Date date = new Date();    System.out.println(dtf1.format(date));    System.out.println(dtf2.format(date));    System.out.println(dtf3.format(date));}@Testpublic void fun2() {    //只显示年月日    DateFormat dtf1 = DateFormat.getDateInstance(DateFormat.FULL);    //只显示时分秒    DateFormat dtf2 = DateFormat.getTimeInstance(DateFormat.MEDIUM);    //显示年月日和时分秒    DateFormat dtf3 = DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.SHORT);    Date date = new Date();    System.out.println(dtf1.format(date));    System.out.println(dtf2.format(date));    System.out.println(dtf3.format(date));}@Testpublic void fun3() {    //只显示年月日    DateFormat dtf1 = DateFormat.getDateInstance(DateFormat.FULL, Locale.US);    //只显示时分秒    DateFormat dtf2 = DateFormat.getTimeInstance(DateFormat.MEDIUM,Locale.US);    //显示年月日和时分秒    DateFormat dtf3 = DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.SHORT, Locale.US);    Date date = new Date();    System.out.println(dtf1.format(date));    System.out.println(dtf2.format(date));    System.out.println(dtf3.format(date));} //parse方法@Testpublic void fun4() throws ParseException {    //格式化    DateFormat dtf = DateFormat.getDateTimeInstance();    Date date = new Date();    System.out.println(dtf.format(date));    String st = "2017-12-15 11:48:51";    Date d = dtf.parse(st);    System.out.println(d);}

}
`fun4()运行效果
2017-12-15 11:52:03
Fri Dec 15 11:48:51 CST 2017

Process finished with exit code 0


下面是具体发方法的介绍
DateFormat类
1.DateFormat 类可以将一个日期/时间对象格式化为表示某个国家地区的日期/时间字符串。

2.DateFormat 类除了可按国家地区格式化输出日期外,它还定义了一些用于描述日期/时间的显示模式的 int 型的常量,包括FULL, LONG, MEDIUM, DEFAULT, SHORT,实例化DateFormat对象时,可以使用这些常量,控制日期/时间的显示长度。
3.实例化DateFormat类有九种方式,以下三种为带参形式,下面列出的三种方式也可以分别不带参,或只带显示样式的参数。
1)getDateInstance(int style, Locale aLocale):以指定的日期显示模式和本地信息来获得DateFormat实例对象,该实例对象不处理时间值部分。
2)getTimeInstance(int style, Locale aLocale):以指定的时间显示模式和本地信息来获得DateFormat实例对象,该实例对象不处理日期值部分。
3)getDateTimeInstance(int dateStyle, int timeStyle, Locale aLocale):以单独指定的日期显示模式、时间显示模式和本地信息来获得DateFormat实例对象。


NumberFormat类
获得处理整数的NumberFormat实例对象
1)getCurrencyInstance(Locale locale):以参数locale对象所标识的本地信息来获得处理实例化NumberFormat类时,可以使用locale对象作为参数,也可以不使用,下面列出的是使用参数的。
2)getNumberInstance(Locale locale):以参数locale对象所标识的本地信息来获得具有多种用途的NumberFormat实例对象
3)getIntegerInstance(Locale locale):以参数locale对象所标识的本地信息来货币的NumberFormat实例对象
4) getPercentInstance(Locale locale):以参数locale对象所标识的本地信息来获得处理百分比数值的NumberFormat实例对象

实例如下:

package cn.test;import org.junit.Test;import java.text.NumberFormat;import java.util.Locale;public class NumberFormatTest {    //数值操作    @Test    public void test1() {        NumberFormat nf = NumberFormat.getIntegerInstance();        String s1 = nf.format(19.6326512);        System.out.println(s1);    }    //查百分比    @Test    public void fun2() {        NumberFormat nf1 = NumberFormat.getPercentInstance();        String s = nf1.format(0.92);        System.out.println(s);    }    //货币    @Test    public void fun3() {        NumberFormat nf2 = NumberFormat.getCurrencyInstance(Locale.US);        String money = nf2.format(1000);        System.out.println(money);    }}
原创粉丝点击