java.text--format

总体的来说formate主要涉及到三个方法。即为format(),parse(),及中间配合Local的使用
1.NumberFormat常用格式化语句:
1.1 DecimalFormat的基本使用

NumberFormat format = new DecimalFormat("000000");;
String s = format.format(-1234.567); //-001235

NumberFormat format1 = new DecimalFormat("##");
String ss = format1.format(-1234.567); //-001235


NumberFormat format2 = new DecimalFormat(".00");
String sString = format2.format(0.567); //.57

NumberFormat format3 = new DecimalFormat("0.00");
String string2 = format3.format(0.567); //0.57

NumberFormat format4 = new DecimalFormat("#.#");
String s1 = format4.format(1234.567); //1234.6

NumberFormat format5 = new DecimalFormat("#.000000");
String s3 = format5.format(1234.567); //1234.567000

NumberFormat format6 = new DecimalFormat("#,###,###");
String s4 = format6.format(1234.567); //1235

format6 = new DecimalFormat("'$'#");
String s5 = format6.format(-1234.567); //-$1235

format6 = new DecimalFormat("'abc'#");
s5 = format6.format(-1234.567); //-abc1235


//不同国家地区对于数值的格式化方式是不一样的
Locale locale = Locale.CANADA;
String s = NumberFormat.getInstance(locale).format(-1234.56);//-1,234.56
locale = Locale.GERMAN;
s = NumberFormat.getInstance(locale).format(-1234.56); //-1.234,56
s = NumberFormat.getInstance().format(-1234.56); //-1,234.56

try
{
Number number = NumberFormat.getInstance(Locale.GERMAN).parse("-1.234,56");
if(number instanceof Long){
System.out.println("it's Long type");
}else if(number instanceof Double){
System.out.println("it's Double type"); //在这行输出结果
}
}
catch (ParseException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}


1.2取得货币的格式化

Locale locale = Locale.CANADA;
String string = NumberFormat.getCurrencyInstance(locale).format(123.45);//$123.45 这里是货币的格式化方式

1.3取得百分比的格式化

Locale locale1 = Locale.CANADA;
String pp = NumberFormat.getPercentInstance(locale1).format(123.456); //这里是百分比的格式化形式

2.DateFormat的基本使用

2.1基本使用

DateFormat dFormat= new SimpleDateFormat("HH:mm:ss a"); //a 会根据不同的地区或国家来显示不同的时段,在中国就是上午的意思
String pp = dFormat.format(new Date()); //08.59.28 上午
若是我们在 DateFormat dFormat= new SimpleDateFormat("HH:mm:ss a",Locale.ENGLISH);那么出现的就是
String pp = dFormat.format(new Date()); //08.59.28 AM
也就是说会根据不同的地区来显示。

df = new SimpleDateFormat("HH:mm:ss Z");可以加上Local.ENGLISH
Date date2 = (Date)df.parse("22:14:02 -0500");//Fri Jan 02 11:14:02 CST 1970 Z代表时区的概念

2.2取得在一周是星期几

DateFormat dFormat = new SimpleDateFormat("EEEE",Locale.CHINA);//若是在默认的情况下就是取得默认地区的形式
System.out.println(dFormat.format(new Date())); //星期三

2.3 SimpleDateFormat的基本使用
若是

java.text.SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.CHINA);// 表示24小时制
java.text.SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss",Locale.CHINA);//表示12小时制

2.4 常用转化以 将一个String类型的数值转化为DATE类型。

DateFormat df = new SimpleDateFormat("yyyyMMddHHhhss"); ————这里最关键
Date pp = df.parse("20081002102030");
DateFormat p1p = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(p1p.format(pp));

// 如此即可以把一个String类型的转化为自己需要形式的String类型

3.MessageFormat的使用

Object[] params = new Object[]{new Integer(123), new Integer(1234)};
String msg = MessageFormat.format("There are {0} a''s and {1} b''s", params);
// There are 123 a's and 1,234 b's

msg = MessageFormat.format("There are {0,number} a''s and {1,number} b''s", params);
// There are 123 a's and 1,234 b's

// Use a custom format; see e311 Formatting a Number Using a Custom Format
msg = MessageFormat.format("There are {0,number,#} a''s and {1,number,#} b''s", params);
// There are 123 a's and 1234 b's

// Floating point numbers
params = new Object[]{new Double(123.45), new Double(1234.56)};
msg = MessageFormat.format("There are {0,number,#.#} a''s and {1,number,#.#} b''s", params);
// There are 123.4 a's and 1234.6 b's

// Currency
msg = MessageFormat.format("There are {0,number,currency} a''s and {1,number,currency} b''s", params);
// There are $123.00 a's and $1,234.00 b's

// Percent
msg = MessageFormat.format("There are {0,number,percent} a''s and {1,number,percent} b''s", params);
// There are 12,345% a's and 123,456% b's
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值