1、生成随机字符串
//length表示生成字符串的长度
public static String getRandomStr(int length) {
String source = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
Random random = new Random();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < length; i++) {
int number = random.nextInt(source .length());
sb.append(source .charAt(number));
}
return sb.toString();
}
2、Android检测手机是否安装了某个应用
public static boolean isInstalled(Context context, String packageName) {
PackageManager pm = context.getPackageManager();
try {
pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
return true;
} catch (PackageManager.NameNotFoundException e) {
return false;
}
}
3、字符串相似度判断
public static float getSimilarityRatio(String str, String target) {
int d[][]; // 矩阵
int n = str.length();
int m = target.length();
int i; // 遍历str的
int j; // 遍历target的
char ch1; // str的
char ch2; // target的
int temp; // 记录相同字符,在某个矩阵位置值的增量,不是0就是1
if (n == 0 || m == 0) {
return 0;
}
d = new int[n + 1][m + 1];
for (i = 0; i <= n; i++) { // 初始化第一列
d[i][0] = i;
}
for (j = 0; j <= m; j++) { // 初始化第一行
d[0][j] = j;
}
for (i = 1; i <= n; i++) { // 遍历str
ch1 = str.charAt(i - 1);
// 去匹配target
for (j = 1; j <= m; j++) {
ch2 = target.charAt(j - 1);
if (ch1 == ch2 || ch1 == ch2 + 32 || ch1 + 32 == ch2) {
temp = 0;
} else {
temp = 1;
}
// 左边+1,上边+1, 左上角+temp取最小
d[i][j] = Math.min(Math.min(d[i - 1][j] + 1, d[i][j - 1] + 1), d[i - 1][j - 1] + temp);
}
}
return (1 - (float) d[n][m] / Math.max(str.length(), target.length())) * 100F;
}
4、时间转多久之前
public static String getShortTime(String dateStr) {
String shortstring = null;
String time = timestampToStr(Long.parseLong(dateStr));
Date date = getDateByString(time);
if (date == null) return shortstring;
long now = Calendar.getInstance().getTimeInMillis();
long deltime = (now - date.getTime()) / 1000;
if (deltime > 365 * 24 * 60 * 60) {
shortstring = (int) (deltime / (365 * 24 * 60 * 60)) + "年前";
} else if (deltime > 30 * 24 * 60 * 60) {
shortstring = (int) (deltime / (30 * 24 * 60 * 60)) + "月前";
}else if (deltime > 24 * 60 * 60) {
shortstring = (int) (deltime / (24 * 60 * 60)) + "天前";
} else if (deltime > 60 * 60) {
shortstring = (int) (deltime / (60 * 60)) + "小时前";
} else if (deltime > 60) {
shortstring = (int) (deltime / (60)) + "分前";
} else if (deltime > 1) {
shortstring = deltime + "秒前";
} else {
shortstring = "1秒前";
}
return shortstring;
}
5、隐藏手机号中间数字为星号
public static String phoneFormat(String str) {
if (TextUtils.isEmpty(str)) return "";
if (str.length() < 8) return str;
String startStr = str.substring(0, 3);
String endStr = str.substring(7);
StringBuilder sb = new StringBuilder();
sb.append(startStr).append("****").append(endStr);
return sb.toString();
}
6、证件号中部分间隐藏
public static String phoneIdCardFormat(String phone) {
if (TextUtils.isEmpty(phone)) return "";
if (phone.length() < 6) return phone;
String endStr = "";
String startStr = "";
if (phone.length() % 2 == 0) {
if (phone.length() <= 10) {
startStr = phone.substring(0, (phone.length() - 5) / 2);
endStr = phone.substring(phone.length() - (phone.length() - 1) / 2, phone.length());
} else {
startStr = phone.substring(0, (phone.length() - 5) / 2);
endStr = phone.substring(phone.length() - (phone.length() - 10) / 2, phone.length());
}
} else {
if (phone.length() <= 10) {
startStr = phone.substring(0, (phone.length() - 1 - 4) / 2);
endStr = phone.substring(phone.length() - (phone.length()) / 2, phone.length());
} else {
startStr = phone.substring(0, (phone.length() - 1 - 4) / 2);
endStr = phone.substring(phone.length() - (phone.length() + 1 - 11) / 2, phone.length());
}
}
StringBuilder sb = new StringBuilder();
sb.append(startStr).append("****").append(endStr);
return sb.toString();
}
// cmd 查看当前Android设备的 CPU 架构
adb shell getprop ro.product.cpu.abi