convertutil.java

来自「一个很好的jbpm应用实例」· Java 代码 · 共 82 行

JAVA
82
字号
package c20.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 类型转换工具类
 * 
 * @author yuxd
 * 
 */
public class ConvertUtil {
    private static final Log log = LogFactory.getLog(ConvertUtil.class);
	static DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
	static DateFormat tf = new SimpleDateFormat("HH时mm分ss秒");
	
	/**
	 * 转换日期为字符串
	 * @param date
	 * @return
	 */
	public static String convertString(Date date){
		
		return df.format(date);
	}
	
	/**
	 * 转换字符串为日期
	 * @param sdate
	 * @return
	 * @throws ConversionException
	 */
	public static Date convertDate(String sdate)throws ConversionException{
		try {
			return df.parse(sdate);
		} catch (ParseException e) {
			log.error(e);
			throw new ConversionException("转换字符串" + sdate + "到日期错误");
		}
	}
	
	public static int convertInt(String value) throws ConversionException{
		try{
			return Integer.parseInt(value);
		}catch(NumberFormatException e){
			log.error(e);
			throw new ConversionException("转换字符串" + value + "到整数错误");
		}
	}
	
	/**
	 * 转换整数为小数形式#.##
	 * @param value
	 * @return
	 */
	public static String convertCurrency(int value){
		int ivalue = value/100;
		int fvalue = value%100;
		if(fvalue < 10){
			return ivalue + ".0" + fvalue; 
		}else{
			return ivalue + "." + fvalue; 
		}
	}

	/**
	 * 转换时间为字符串
	 * @param date
	 * @return
	 */
	public static String convertTimeString(Date date){
		
		return tf.format(date);
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?