安卓工具类一个,自己多个项目总结下来的,分享给大家



/**
 * 工具类:包括最常用的字符串为空的检验,时间的格式化,数字的格式化,取精度,sql语句拼接等
 * 
 * @author 包子大叔
 * @time 2013-8-29 上午10:40:57
 */
public class Util {

	/**
	 * 检查字符串是否是空对象或空字符串
	 * 
	 * @param str
	 * @return 为空返回true,不为空返回false
	 */
	public static boolean isNull(String str) {
		if (null == str || "".equals(str) || "null".equalsIgnoreCase(str)) {
			return true;
		} else {
			return false;
		}
	}

	/*
	 * px:像素。 in:英寸。 mm:毫米。 pt:磅。 dp:与密度无关的像素,基于160dpi(每英寸的像素数)屏幕(尺寸适应屏幕密度)。
	 * sp:与比例无关的像素(这种尺寸支持用户调整大小,适合在字体中使用)。
	 */

	/**
	 * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
	 */
	public static int dip2px(Context context, float dpValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dpValue * scale + 0.5f);
	}

	/**
	 * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
	 */
	public static int px2dip(Context context, float pxValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (pxValue / scale + 0.5f);
	}

	// --------------------------------------time--------------------------------------//
	/**
	 * 获取当前时间, 格式"yyyy-MM-dd"
	 * 
	 * @return
	 */
	@SuppressLint("SimpleDateFormat")
	public static String timeFormatDateShort(Date d) {
		return new SimpleDateFormat("yyyy-MM-dd").format(d);
	}

	/**
	 * 获取当前时间, 格式"yyyy-MM-dd HH:mm:ss"
	 * 
	 * @return
	 */
	public static String timeFormatTime() {
		Date d = new Date();
		return timeFormat(d);
	}

	/**
	 * 获取当前时间, 格式"yyyy-MM-dd 00:00:00"
	 * 
	 * @return
	 */
	@SuppressLint("SimpleDateFormat")
	public static String timeFormatDateBegin(Date d) {
		String str = new SimpleDateFormat("yyyy-MM-dd").format(d);
		return str + " 00:00:00";
	}

	/**
	 * 获取当前时间, 格式"yyyy-MM-dd 23:59:59"
	 * 
	 * @return
	 */
	@SuppressLint("SimpleDateFormat")
	public static String timeFormatDateEnd(Date d) {
		String str = new SimpleDateFormat("yyyy-MM-dd").format(d);
		return str + " 23:59:59";
	}

	/**
	 * 获取当前时间, 格式"yyyy-MM-dd HH:mm:ss"
	 * 
	 * @return
	 */
	@SuppressLint("SimpleDateFormat")
	public static String timeFormat(Date d) {
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(d);
	}

	/**
	 * 获取当前时间, 格式"yyyy-MM-dd HH:mm:ss"
	 * 
	 * @param fomatStr
	 *            格式化字符串
	 * @return
	 */
	@SuppressLint("SimpleDateFormat")
	public static String timeFormat(String fomatStr) {
		return new SimpleDateFormat(fomatStr).format(new Date());
	}

	/**
	 * 获取当前时间, 格式"yyyy-MM-dd HH:mm:ss"
	 * 
	 * @param fomatStr
	 *            格式化字符串
	 * @return
	 */
	@SuppressLint("SimpleDateFormat")
	public static String timeFormat(String fomatStr, Date d) {
		return new SimpleDateFormat(fomatStr).format(d);
	}

	// ------------------------------------------Math-------------------------------------//
	/**
	 * 金钱格式化,取到小数点后两位,例如:0.00
	 * 
	 * @param money
	 * @return
	 */
	public static String moneyFormat(double money) {
		DecimalFormat df = new DecimalFormat("0.00");
		return df.format(money);
	}

	/**
	 * 去掉多余的.与0
	 * 
	 * @param s
	 *            数字型字符串
	 * @return
	 */
	public static String subZeroAndDot(String s) {
		if (s.indexOf(".") > 0) {
			s = s.replaceAll("0+?$", "");// 去掉多余的0
			s = s.replaceAll("[.]$", "");// 如最后一位是.则去掉
		}
		return s;
	}

	/**
	 * 去掉多余的.与0
	 * 
	 * @param d
	 * @return
	 */
	public static String subZeroAndDot(double d) {
		return subZeroAndDot(d + "");
	}

	/**
	 * 取数值字符串的整数部分
	 */
	public static int toInt(String strNumber) {
		return (int) Math.floor(toDouble(strNumber));
	}

	public static double toDouble(String strNumber) {
		try {
			return Double.parseDouble(strNumber);
		} catch (Exception e) {
			return 0;
		}
	}

	/**
	 * 精确到两位小数位
	 * 
	 * @param strNumber
	 * @return
	 */
	public static String getRound2(String strNumber) {
		try {

			double number = Double.parseDouble(strNumber);
			return getRound2(number) + "";
		} catch (Exception e) {
			return "0.00";
		}
	}

	/**
	 * 精确到两位小数位
	 * 
	 * @param strNumber
	 * @return
	 */
	public static double getRound2(double number) {
		return Arith.round(number, 2);
	}

	/**
	 * 精确到小数点后N,四舍五入
	 * 
	 * @param number
	 * @param n
	 * @return
	 */
	public static double getRound(double number, int n) {
		return Arith.round(number, n);
	}

	// ----------------------------------------sql--------------------------------------//
	/**
	 * 对字符串类型的参数进行格式化, 将会自动给值添加引号, "abc"-->"'abc'" <br>
	 * 本方法会将字符串内的 [单引号] 格式化为 [双引号] 以防止数据库报错, "a'b'c"-->"'a''b''c'"
	 * 
	 * @param str
	 *            "abc"
	 * @return "'abc'"
	 */
	public static String sqlParamFomat(String str) {
		if (null != str) {
			String tmp = str.replace("'", "''");
			return "'" + tmp + "'";
		} else {
			return "null";
		}
	}

	/**
	 * 传入指定格式的sql字符串和参数列表,返回拼接好的sql语句 例如: <br>
	 * sql: select * from stock where companyid={0}<br>
	 * 其中{0}将被参数列表中的第0个参数替换<br>
	 * 本函数会对参数进行参数化处理,请优先使用本函数拼接sql <br>
	 * 目前支持的参数类型: String,Integer,Long,Float,Double
	 * 
	 * @param sqlStr
	 * @param arr
	 * @return
	 */
	public static String sqlCreate(String sqlStr, Object[] arr) {
		String newSql = sqlStr;
		int len = arr.length;
		for (int i = 0; i < len; i++) {
			Object obj = arr[i];
			if (obj instanceof String) {
				// 字符串要进行格式化
				String param = (String) obj;
				newSql = sqlStr.replace("{" + i + "}", sqlParamFomat(param));
			}
			if ((obj instanceof Integer) || (obj instanceof Long)
					|| (obj instanceof Float) || (obj instanceof Double)) {
				newSql = sqlStr.replace("{" + i + "}", obj + "");
			}
			if ((obj instanceof Calendar)) {
				Calendar param = (Calendar) obj;
				newSql = sqlStr.replace("{" + i + "}",
						timeFormat(param.getTime()));
			}
			if ((obj instanceof Date)) {
				Date param = (Date) obj;
				newSql = sqlStr.replace("{" + i + "}", timeFormat(param));
			}
		}
		return newSql;
	}

	public static String sqlCreate(String sqlStr, String[] arr) {
		String newSql = sqlStr;
		int len = arr.length;
		for (int i = 0; i < len; i++) {
			newSql = sqlStr.replace("{" + i + "}", sqlParamFomat(arr[i]));
		}
		return newSql;
	}

	// ----------------------------------------other--------------------------------------//
	/**
	 * 检测网络连接是否可用
	 * 
	 * @param ctx
	 * @return true 可用; false 不可用
	 */
	public static boolean isNetworkAvailable(Context ctx) {
		ConnectivityManager cm = (ConnectivityManager) ctx
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (cm == null) {
			return false;
		}
		NetworkInfo activeNetInfo = cm.getActiveNetworkInfo();
		if (activeNetInfo != null) {
			if (activeNetInfo.isAvailable()) {
				return true;
			} else {
				return false;
			}
		}
		return false;
	}

}

猜你喜欢

转载自zheyiw.iteye.com/blog/1933172
今日推荐