109 lines
3.6 KiB
Java
109 lines
3.6 KiB
Java
package com.hivekion.common;
|
|
|
|
import com.hivekion.common.exception.BusinessException;
|
|
|
|
|
|
import net.sourceforge.pinyin4j.PinyinHelper;
|
|
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
|
|
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
|
|
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
|
|
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
|
|
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
|
|
|
|
|
|
import java.text.MessageFormat;
|
|
import java.time.Duration;
|
|
import java.util.List;
|
|
import java.util.stream.Collectors;
|
|
|
|
/**
|
|
* 一些公用的方法集
|
|
*/
|
|
public class utils {
|
|
|
|
/**
|
|
* 取差集
|
|
*/
|
|
public static List<String> diffList(List<String> firstArrayList, List<String> secondArrayList) {
|
|
if (firstArrayList == null) {
|
|
return secondArrayList;
|
|
}
|
|
List<String> resultList =
|
|
firstArrayList.stream()
|
|
.filter(item -> !secondArrayList.contains(item))
|
|
.distinct()
|
|
.collect(Collectors.toList());
|
|
return resultList;
|
|
}
|
|
|
|
public static String getPinyin(String china) {
|
|
HanyuPinyinOutputFormat formart = new HanyuPinyinOutputFormat();
|
|
formart.setCaseType(HanyuPinyinCaseType.LOWERCASE);
|
|
formart.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
|
|
formart.setVCharType(HanyuPinyinVCharType.WITH_V);
|
|
char[] arrays = china.trim().toCharArray();
|
|
String result = "";
|
|
try {
|
|
for (int i = 0; i < arrays.length; i++) {
|
|
char ti = arrays[i];
|
|
if (Character.toString(ti).matches("[\\u4e00-\\u9fa5]")) { // 匹配是否是中文
|
|
String[] temp = PinyinHelper.toHanyuPinyinStringArray(ti, formart);
|
|
result += temp[0];
|
|
} else {
|
|
result += ti;
|
|
}
|
|
}
|
|
} catch (BadHanyuPinyinOutputFormatCombination e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public static int checkPassword(String password) {
|
|
int level = 0;
|
|
//数字ASCII 48-57 大写65-90 小写97-122
|
|
if (password.isEmpty()) {
|
|
throw new BusinessException(500, "密码不能为空");
|
|
}
|
|
char arr[] = password.toCharArray();
|
|
for (int i = 0; i < arr.length; i++) {
|
|
if (arr[i] >= 48 && arr[i] <= 57) {
|
|
level = level + 1;
|
|
break;
|
|
}
|
|
}
|
|
for (int i = 0; i < arr.length; i++) {
|
|
if (arr[i] >= 65 && arr[i] <= 90) {
|
|
level = level + 1;
|
|
break;
|
|
}
|
|
}
|
|
for (int i = 0; i < arr.length; i++) {
|
|
if (arr[i] >= 97 && arr[i] <= 122) {
|
|
level = level + 1;
|
|
break;
|
|
}
|
|
}
|
|
for (int i = 0; i < arr.length; i++) {
|
|
if ((arr[i] > 20 && arr[i] < 48) || (arr[i] > 57 && arr[i] < 65) || (arr[i] > 90 && arr[i] < 97) || arr[i] > 122 && arr[i] <= 126) {
|
|
level = level + 1;
|
|
break;
|
|
}
|
|
}
|
|
return level;
|
|
}
|
|
public static String formatSeconds(long totalSeconds) {
|
|
|
|
Duration duration = Duration.ofSeconds(totalSeconds);
|
|
long hours = duration.toHours();
|
|
long minutes = duration.minusHours(hours).toMinutes();
|
|
long seconds = duration.minusHours(hours).minusMinutes(minutes).getSeconds();
|
|
return MessageFormat.format("{0}小时{1}分钟{2}秒", hours, minutes, seconds);
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
System.out.println(utils.formatSeconds(3601));
|
|
}
|
|
}
|