公共方法项目总结集锦

1、控制浮点型精度
/**
  *@ param scale 精度控制,保留几位小数
  */
public static  float round(float value, int scale) {

		BigDecimal bd = new BigDecimal(value);

		bd = bd.setScale(scale, BigDecimal.ROUND_UP);

		float d = bd.floatValue();

		bd = null;

		return d;
}

2、时间类型公共方法总结
/**
	 * 所得给定日期的年份
	 * @param date 给定的日期
	 * @return	年份
	 */
	public static int getYear(Date date){
		Calendar ca = Calendar.getInstance(); 
		ca.setTime(date); 
		int year = ca.get(Calendar.YEAR);
		return year;
	}
	/**
	 * 获得给定日期的月份
	 * @param date	给定的日期
	 * @return	月份
	 */
	public static int getMonth(Date date){
		Calendar ca = Calendar.getInstance(); 
		ca.setTime(date); 
		int month = ca.get(Calendar.MONTH);
		return month;
	}
    /**
     *  将长时间格式时间转换为字符串 yyyy-MM-dd HH:mm:ss
          * 此方法主要用于更新SQLSERVER数据库时间时,格式不匹配。避免抛异常
	 * @return 返回时间类型 yyyy-MM-dd HH:mm:ss
	 */
	public static String dateParseFormat(Date date){

		   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		   String dateString = formatter.format(date);
		   return dateString;
	}



3、编码转化(此处是将utf-8的编码转化为ISO-8859-1类型的编码)
public static String getChinese(String in){
		String out="";
		try{
			out=new String(in.getBytes("iso-8859-1"),"utf-8");
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return out;
}

你可能感兴趣的:(总结)