例1: str(arg1,"yyyy年 MM 月dd 日"), 其中 arg1 为日期型参数,值为 1972-09-09,返回值:"1972年 09 月 09日"
例2:str(3456.9876,"¥#,##0.00") 返回值:"¥3,456.99"
/**
* 格式化输出数据,目前只支持字符和数字
* @param obj
* @param format
* @return
* @throws ParseException
*/
public static String format(Object obj,String format) throws ParseException
{
String formatResult = "";
if(null != obj && null != format)
{
if(obj.getClass().getName().equals(int.class.getName())
||obj.getClass().getName().equals(Integer.class.getName())
|| obj.getClass().getName().equals(long.class.getName())
|| obj.getClass().getName().equals(Long.class.getName())
|| obj.getClass().getName().equals(byte.class.getName())
|| obj.getClass().getName().equals(Byte.class.getName())
|| obj.getClass().getName().equals(float.class.getName())
|| obj.getClass().getName().equals(Float.class.getName())
|| obj.getClass().getName().equals(double.class.getName())
|| obj.getClass().getName().equals(Double.class.getName())
|| obj.getClass().getName().equals(short.class.getName())
|| obj.getClass().getName().equals(Short.class.getName()))
{
// 格式化数据
DecimalFormat nf = new DecimalFormat(format);
formatResult = nf.format(obj);
}
else if(obj.getClass().getName().equals(Date.class.getName()))
{
// 格式化日期
SimpleDateFormat df = new SimpleDateFormat(format);
return df.format(obj);
}
else
{
DateFormat dft = DateFormat.getDateInstance();
Date date = dft.parse(String.valueOf(obj).toString());
// 格式化日期
SimpleDateFormat df = new SimpleDateFormat(format);
return df.format(date);
}
}
return formatResult;
}