/**
* 根据经纬度来计算距离 -- 返回单位是米
*
* @param longitude1
* @param latitude1
* @param longitude2
* @param latitude2
* @return
*/
public double getDistance(double longitude1, double latitude1, double longitude2, double latitude2) {
double EARTH_RADIUS = 6378137.0;
double Lat1 = rad(latitude1);
double Lat2 = rad(latitude2);
double a = Lat1 - Lat2;
double b = rad(longitude1) - rad(longitude2);
double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(Lat1) * Math.cos(Lat2) * Math.pow(Math.sin(b / 2), 2)));
s = s * EARTH_RADIUS;
s = Math.round(s * 10000) / 10000;
return s / 1000;
}
/**
* 验证手机格式
* @param mobiles
* @return boolean
*/
public static boolean isMobileNO(String mobiles) {
String telRegex = "[1][358]\\d{9}";
if (TextUtils.isEmpty(mobiles))
return false;
else
return mobiles.matches(telRegex);
}
/**
* md5加密
*
* @param string 加密后的字符串
* @return
*/
public static String md5(String string) {
byte[] hash;
try {
hash = MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8"));
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("Huh, MD5 should be supported?", e);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("Huh, UTF-8 should be supported?", e);
}
StringBuilder hex = new StringBuilder(hash.length * 2);
for (byte b : hash) {
if ((b & 0xFF) < 0x10)
hex.append("0");
hex.append(Integer.toHexString(b & 0xFF));
}
return hex.toString();
}
/**
* 将汉字转换为全拼
*
* @param src
* @return
*/
public static String getPinYin(String src) {
char[] t1 = null;
String t4 = "";
try {
t1 = src.toCharArray();
String[] t2 = new String[t1.length];
HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
t3.setVCharType(HanyuPinyinVCharType.WITH_V);
int t0 = t1.length;
try {
for (int i = 0; i < t0; i++) {
if (Character.toString(t1[i]).matches("[\\u4E00-\\u9FA5]+")) {
t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);
t4 += t2[0];
} else {
t4 += Character.toString(t1[i]);
}
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
return t4.toUpperCase();
}
/**
* 判断是否是中文
*
* @param str
* @return
*/
public static boolean isChineseChar(String str) {
boolean temp = false;
if (str != null && str.length() > 0) {
char[] chars = str.toCharArray();
for (int i = 0; i < chars.length; i++) {
Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
Matcher m = p.matcher(chars[i] + "");
if (m.find()) {
temp = true;
} else {
temp = false;
break;
}
}
}
return temp;
}
/**
* 判断是否是double
* @param value
* @return
*/
public static boolean isBigDecimal(String value) {
try {
Double.parseDouble(value);
return true;
} catch (NumberFormatException e) {
return false;
}
}
/**
* 判断是否是int
* @param value
* @return
*/
public static boolean isInteger(String value) {
try {
Integer.parseInt(value);
return true;
} catch (NumberFormatException e) {
return false;
}
}
/**
* 判断是否是数字
* @param value
* @return
*/
public static boolean isNumber(String value) {
Boolean strResult = value.matches("-?[0-9]+.*[0-9]*");
if(strResult == true) {
return true;
} else {
return false;
}
}
/**
* 保留两位小数
* @param value
* @return
*/
public static String getDecimalFormat(double value) {
DecimalFormat df1 = new DecimalFormat("###.00");
return df1.format(value);
}
/**
* 检测Sdcard是否存在
*
* @return
*/
public static boolean hasSdcard() {
String state = Environment.getExternalStorageState();
if (state.equals(Environment.MEDIA_MOUNTED)) {
return true;
} else {
return false;
}
}
/**
* 获取版本号
* @param context
* @return
*/
public static int getAppVersionCode(Context context) {
try {
PackageInfo info = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
return info.versionCode;
} catch (NameNotFoundException e) {
e.printStackTrace();
}
return 1;
}
/**
* 获取版本名称
* @param context
* @return
*/
public static String getAppVersionName(Context context) {
try {
PackageInfo info = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
return info.versionName;
} catch (NameNotFoundException e) {
e.printStackTrace();
}
return "";
}
/**
* 判断当前日期是星期几
* @param date 修要判断的时间
* @return
*/
public static String getWeek(Date date){
String[] weeks = {"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1;
if(week_index < 0){
week_index = 0;
}
return weeks[week_index];
}