记录一些常用的函数_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 记录一些常用的函数

记录一些常用的函数

 2013/11/22 18:29:00  dai_lm  程序员俱乐部  我要评论(0)
  • 摘要:publicstaticStringconvertInputStreamToString(InputStreamis){StringBuilderresult=newStringBuilder();if(is!=null)try{InputStreamReaderinputReader=newInputStreamReader(is);BufferedReaderbufReader=newBufferedReader(inputReader);Stringline="";while(
  • 标签:常用 函数
class="java">
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;
}
发表评论
用户名: 匿名