SimpleDateFormat自定义工具类TimeUtil + PinYin4JUtil
GitHub: link1.link2. 欢迎star
一、TimeUtil.java
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class TimeUtil {
private static final Logger LOG = LoggerFactory.getLogger(TimeUtil.class);
public static final ThreadLocal<SimpleDateFormat> FORMAT = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
public static final ThreadLocal<SimpleDateFormat> FORMAT_DAY = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd"));
public static final ThreadLocal<SimpleDateFormat> FORMAT_HOUR = ThreadLocal.withInitial(() -> new SimpleDateFormat("HH"));
public static final ThreadLocal<SimpleDateFormat> FORMAT_MINUTE = ThreadLocal.withInitial(() -> new SimpleDateFormat("mm"));
public static final ThreadLocal<SimpleDateFormat> FORMAT_SECOND = ThreadLocal.withInitial(() -> new SimpleDateFormat("ss"));
public static long getTimeId(String time) {
long timeId = 0;
try {
Date date = FORMAT.get().parse(time);
timeId = date.getTime() / 1000;
} catch (ParseException e) {
LOG.error("FORMAT parse time failure!! error={}", e.getMessage());
}
return timeId;
}
public static Date parseTime(String time) {
Date parse = null;
try {
parse = TimeUtil.FORMAT.get().parse(time);
} catch (ParseException e) {
LOG.error("FORMAT parse time failure!! error={}", e.getMessage());
}
return parse;
}
public static void sleep(long timestamp) {
try {
Thread.sleep(timestamp);
} catch (InterruptedException e) {
LOG.error("Thread sleep failure!! error={}", e.getMessage());
}
}
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];
}
}
二、PinYin4JUtil.java
<dependency>
<groupId>com.belerweb</groupId>
<artifactId>pinyin4j</artifactId>
<version>2.5.1</version>
</dependency>
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;
public class PinYin4JUtil {
public static String getPingYin(String src) {
char[] t1;
t1 = src.toCharArray();
String[] t2;
HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
t3.setVCharType(HanyuPinyinVCharType.WITH_V);
StringBuilder t4 = new StringBuilder();
int t0 = t1.length;
try {
for (char c : t1) {
if (Character.toString(c).matches("[\\u4E00-\\u9FA5]+")) {
t2 = PinyinHelper.toHanyuPinyinStringArray(c, t3);
t4.append(t2[0]);
} else {
t4.append(c);
}
}
return t4.toString();
} catch (BadHanyuPinyinOutputFormatCombination e1) {
e1.printStackTrace();
}
return t4.toString();
}
public static String getPinYinHeadChar(String str) {
StringBuilder convert = new StringBuilder();
for (int j = 0; j < str.length(); j++) {
char word = str.charAt(j);
String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
if (pinyinArray != null) {
convert.append(pinyinArray[0].charAt(0));
} else {
convert.append(word);
}
}
return convert.toString();
}
public static String getCnASCII(String cnStr) {
StringBuilder strBuf = new StringBuilder();
byte[] bGBK = cnStr.getBytes();
for (byte b : bGBK) {
strBuf.append(Integer.toHexString(b & 0xff));
}
return strBuf.toString();
}
}
GitHub: link1.link2. 欢迎star