记录一些常用的函数

public static String convertInputStreamToString(InputStream is) {

	StringBuilder result = new StringBuilder();

	if (is != null)
		try {
			InputStreamReader inputReader = new InputStreamReader(is);
			BufferedReader bufReader = new BufferedReader(inputReader);
			String line = "";
			while ((line = bufReader.readLine()) != null)
				result.append(line);

			bufReader.close();
			inputReader.close();

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	return result.toString();
}

public static boolean isEmpty(String string) {
    return string == null || string.length() == 0;
}

public static boolean isNumeric(String strNumeric) {
	if (isEmpty(strNumeric))
		return false;

	String patternStr = "^[-+]?\\d+(\\.\\d+)?$";

	if (Pattern.matches(patternStr, strNumeric))
		return true;
	else
		return false;
}

public static float getDistance(double lat1, double lon1, double lat2, double lon2) {

    int EARTH_RADIUS_KM = 6371;

	// if there's unavailable location (0,0), return 0
	if (lat1 == 0 || lon1 == 0 || lat2 == 0 || lon2 == 0)
		return 0;

	double lat1Rad = Math.toRadians(lat1);
	double lat2Rad = Math.toRadians(lat2);
	double deltaLonRad = Math.toRadians(lon2 - lon1);

	double km = Math.acos(Math.sin(lat1Rad) * Math.sin(lat2Rad) + Math.cos(lat1Rad)
			* Math.cos(lat2Rad) * Math.cos(deltaLonRad))
			* EARTH_RADIUS_KM;

	return km;
}

public static String getMD5(String val) {
    try {
        byte[] source = val.getBytes("UTF-8");
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        md5.update(source);
        StringBuffer buf = new StringBuffer();
        for (byte b : md5.digest())
            buf.append(String.format("%02x", b & 0xff));
        return buf.toString();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return "";
    }
}

猜你喜欢

转载自dai-lm.iteye.com/blog/1979471
今日推荐