java金额格式化工具类_功能更强大的格式化工具类 FormatUtils.java

package com.util;

import java.text.DecimalFormat;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Date;

/**

* 功能更强大的格式化工具类

*/

public class FormatUtils {

private static SimpleDateFormat second = new SimpleDateFormat(

"yy-MM-dd hh:mm:ss");

private static SimpleDateFormat day = new SimpleDateFormat("yyyy-MM-dd");

private static SimpleDateFormat detailDay = new SimpleDateFormat("yyyy年MM月dd日");

private static SimpleDateFormat fileName = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");

private static SimpleDateFormat tempTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

private static SimpleDateFormat excelDate = new SimpleDateFormat("yyyy/MM/dd");

/**

* 格式化excel中的时间

* @param date

* @return

*/

public static String formatDateForExcelDate(Date date) {

return excelDate.format(date);

}

/**

* 将日期格式化作为文件名

* @param date

* @return

*/

public static String formatDateForFileName(Date date) {

return fileName.format(date);

}

/**

* 格式化日期(精确到秒)

*

* @param date

* @return

*/

public static String formatDateSecond(Date date) {

return second.format(date);

}

/**

* 格式化日期(精确到秒)

*

* @param date

* @return

*/

public static String tempDateSecond(Date date) {

return tempTime.format(date);

}

public static Date tempDateSecond(String str) {

try {

return tempTime.parse(str);

} catch (ParseException e) {

e.printStackTrace();

}

return new Date();

}

/**

* 格式化日期(精确到天)

*

* @param date

* @return

*/

public static String formatDateDay(Date date) {

return day.format(date);

}

/**

* 格式化日期(精确到天)

*

* @param date

* @return

*/

public static String formatDateDetailDay(Date date) {

return detailDay.format(date);

}

/**

* 将double类型的数字保留两位小数(四舍五入)

*

* @param number

* @return

*/

public static String formatNumber(double number) {

DecimalFormat df = new DecimalFormat();

df.applyPattern("#0.00");

return df.format(number);

}

/**

* 将字符串转换成日期

*

* @param date

* @return

* @throws Exception

*/

public static Date formateDate(String date) throws Exception {

return day.parse(date);

}

/**

* 将字符日期转换成Date

* @param date

* @return

* @throws Exception

*/

public static Date parseStringToDate(String date) throws Exception {

return day.parse(date);

}

public static String formatDoubleNumber(double number) {

DecimalFormat df = new DecimalFormat("#");

return df.format(number);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值