oh ,now it's time to sum up the class of "java.text.format" , as i forget how to format a date after using other's tools .
1、数字格式化 DecimalFormat
使用 java.text.format decimalFormat = new DecimalFormat("#.##"); 保留2位小数,允许整数与小数不存在。
使用 java.text.format decimalFormat = new DecimalFormat("0.0#");保留2位小数,无小数,小数位补0 。 无整数,整数位补0
2、日起格式化 simpleDateFormat
使用java.text.format dateFormat = new SimpleDateFormat(pattern) .
pattern : yyyy-MM-dd HH:mm:ss SSS E 表示 2014-12-06 23:59:58 333 星期四 and then ,you can short of the pattern just to suit yourself
注意 : 使用mysql格式化日起保存到数据库的时候,请不要用毫秒,否则会四舍五入, 2014-12-06 23:59:58 500 会保存为 2014-12-07 00:00:00
3、文本替换格式化 MessageFormat
描述:使用一个数组中的元素去替换String(模板)中{ArgumentIndex , FormatType , FormatStyle}中的指定位置
{ArgumentIndex , FormatType , FormatStyle} : {数组下标,格式化数组类型,格式}
例如 : String message = "xiao ming is {0} {1,number,#.##}" , Object array = new Object[]{"high",3.33333} , String value = MessageFormat.format(message,array);
结果 : xiao ming is high 3.33 . message就是模板。
then just thinking the result if the message is "xiao ming is {} " . 是会全部打出呢 还是 说取消大括号 ?
建议使用:new MessageFormat(message).format(array); 因为上面的代码我无法得出正确结论 ,同时下面的代码就是上面代码的解析。。
for more message , just view http://www.xuebuyuan.com/1576172.html